Skip to content

Commit

Permalink
Move some tests out of self.assert_passes (#750)
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra authored Mar 11, 2024
1 parent 2b0e6c1 commit 4dd5205
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 91 deletions.
52 changes: 24 additions & 28 deletions pyanalyze/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,35 +108,31 @@ def capybara(a, *, b, c=3):
def failing_capybara(a, *, b):
capybara(1, 2) # E: incompatible_call

@assert_passes(settings={ErrorCode.missing_parameter_annotation: True})
def test_pos_only(self):
self.assert_passes(
"""
from typing import Optional
def f(a: int, /) -> None:
assert_is_value(a, TypedValue(int))
def g(a: Optional[str] = None, /, b: int = 1) -> None:
assert_is_value(a, TypedValue(str) | KnownValue(None))
assert_is_value(b, TypedValue(int))
def h(a, b: int = 1, /, c: int = 2) -> None: # E: missing_parameter_annotation
assert_is_value(a, AnyValue(AnySource.unannotated))
assert_is_value(b, TypedValue(int))
def capybara() -> None:
f(1)
f("x") # E: incompatible_argument
f(a=1) # E: incompatible_call
g(a=1) # E: incompatible_call
g(b=1)
g(None, b=1)
h(1, 1, c=2)
h(1)
h(1, b=1) # E: incompatible_call
""",
settings={ErrorCode.missing_parameter_annotation: True},
)
from typing import Optional

def f(a: int, /) -> None:
assert_is_value(a, TypedValue(int))

def g(a: Optional[str] = None, /, b: int = 1) -> None:
assert_is_value(a, TypedValue(str) | KnownValue(None))
assert_is_value(b, TypedValue(int))

def h(a, b: int = 1, /, c: int = 2) -> None: # E: missing_parameter_annotation
assert_is_value(a, AnyValue(AnySource.unannotated))
assert_is_value(b, TypedValue(int))

def capybara() -> None:
f(1)
f("x") # E: incompatible_argument
f(a=1) # E: incompatible_call
g(a=1) # E: incompatible_call
g(b=1)
g(None, b=1)
h(1, 1, c=2)
h(1)
h(1, b=1) # E: incompatible_call

@assert_passes()
def test_lambda(self):
Expand Down
81 changes: 32 additions & 49 deletions pyanalyze/test_name_check_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2007,48 +2007,37 @@ def f(self, value: bool) -> None: # E: incompatible_override


class TestWalrus(TestNameCheckVisitorBase):
@assert_passes()
def test(self):
self.assert_passes(
"""
from typing import Optional
from typing import Optional

def opt() -> Optional[int]:
return None
def opt() -> Optional[int]:
return None

def capybara():
if (x := opt()):
assert_is_value(x, TypedValue(int))
assert_is_value(x, TypedValue(int) | KnownValue(None))
def capybara():
if x := opt():
assert_is_value(x, TypedValue(int))
assert_is_value(x, TypedValue(int) | KnownValue(None))

if (y := opt()) is not None:
assert_is_value(y, TypedValue(int))
assert_is_value(y, TypedValue(int) | KnownValue(None))
"""
)
if (y := opt()) is not None:
assert_is_value(y, TypedValue(int))
assert_is_value(y, TypedValue(int) | KnownValue(None))

@assert_passes()
def test_and(self):
self.assert_passes(
"""
from typing import Optional
from typing import Optional, Set

def opt() -> Optional[int]:
return None
def opt() -> Optional[int]:
return None

def capybara(cond):
if (x := opt()) and cond:
assert_is_value(x, TypedValue(int))
assert_is_value(x, TypedValue(int) | KnownValue(None))
"""
)
self.assert_passes(
"""
from typing import Set
def capybara(cond):
if (x := opt()) and cond:
assert_is_value(x, TypedValue(int))
assert_is_value(x, TypedValue(int) | KnownValue(None))

def func(myvar: str, strset: Set[str]) -> None:
if (encoder_type := myvar) and myvar in strset:
print(encoder_type)
"""
)
def func(myvar: str, strset: Set[str]) -> None:
if (encoder_type := myvar) and myvar in strset:
print(encoder_type)

@assert_passes()
def test_and_then_walrus(self):
Expand All @@ -2061,26 +2050,20 @@ def capybara(cond):
print(x) # E: possibly_undefined_name
print(x) # E: possibly_undefined_name

@assert_passes()
def test_if_exp(self):
self.assert_passes(
"""
def capybara(cond):
(x := 2) if cond else (x := 1)
assert_is_value(x, KnownValue(2) | KnownValue(1))
"""
)
def capybara(cond):
(x := 2) if cond else (x := 1)
assert_is_value(x, KnownValue(2) | KnownValue(1))

@assert_passes()
def test_comprehension_scope(self):
self.assert_passes(
"""
from typing import List, Optional
from typing import List, Optional

def capybara(elts: List[Optional[int]]) -> None:
if any((x := i) is not None for i in elts):
assert_is_value(x, TypedValue(int) | KnownValue(None))
print(i) # E: undefined_name
"""
)
def capybara(elts: List[Optional[int]]) -> None:
if any((x := i) is not None for i in elts):
assert_is_value(x, TypedValue(int) | KnownValue(None))
print(i) # E: undefined_name


class TestUnion(TestNameCheckVisitorBase):
Expand Down
25 changes: 11 additions & 14 deletions pyanalyze/test_signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,23 +855,20 @@ def capybara(arg):
typed_int_kwargs = {int(x): 1 for x in arg}
many_args(**typed_int_kwargs) # E: incompatible_call

@assert_passes()
def test_pos_only(self):
self.assert_passes(
"""
from typing import Sequence
from typing import Sequence

def pos_only(pos: int, /) -> None:
pass
def pos_only(pos: int, /) -> None:
pass

def capybara(ints: Sequence[int], strs: Sequence[str]) -> None:
pos_only(1)
pos_only(*ints)
pos_only(*strs) # E: incompatible_argument
pos_only(pos=1) # E: incompatible_call
pos_only() # E: incompatible_call
pos_only(1, 2) # E: incompatible_call
"""
)
def capybara(ints: Sequence[int], strs: Sequence[str]) -> None:
pos_only(1)
pos_only(*ints)
pos_only(*strs) # E: incompatible_argument
pos_only(pos=1) # E: incompatible_call
pos_only() # E: incompatible_call
pos_only(1, 2) # E: incompatible_call

@assert_passes()
def test_kw_only(self):
Expand Down

0 comments on commit 4dd5205

Please sign in to comment.