Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flip the order of the arguments to is_compatible #687

Merged
merged 2 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyanalyze/implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1455,7 +1455,7 @@ def get_default_argspecs() -> Dict[object, Signature]:
callable=assert_is_value,
),
Signature.make(
[SigParameter("typ"), SigParameter("value")],
[SigParameter("value"), SigParameter("typ")],
return_annotation=TypedValue(bool),
impl=_is_compatible_impl,
callable=is_compatible,
Expand Down
16 changes: 8 additions & 8 deletions pyanalyze/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ def _get_checker() -> "pyanalyze.checker.Checker":


@used
def is_compatible(typ: object, value: object) -> bool:
def is_compatible(value: object, typ: object) -> bool:
"""Return whether ``value`` is compatible with ``type``.

Examples::

>>> is_compatible(list[int], 42)
>>> is_compatible(42, list[int])
False
>>> is_compatible(list[int], [])
>>> is_compatible([], list[int])
True
>>> is_compatible(list[int], ["x"])
>>> is_compatible(["x"], list[int])
False

"""
Expand All @@ -38,18 +38,18 @@ def is_compatible(typ: object, value: object) -> bool:


@used
def get_compatibility_error(typ: object, value: object) -> Optional[str]:
def get_compatibility_error(value: object, typ: object) -> Optional[str]:
"""Return an error message explaining why ``value`` is not
compatible with ``type``, or None if they are compatible.

Examples::

>>> print(get_compatibility_error(list[int], 42))
>>> print(get_compatibility_error(42, list[int]))
Cannot assign Literal[42] to list

>>> print(get_compatibility_error(list[int], []))
>>> print(get_compatibility_error([], list[int]))
None
>>> print(get_compatibility_error(list[int], ["x"]))
>>> print(get_compatibility_error(["x"], list[int]))
In element 0
Cannot assign Literal['x'] to int

Expand Down
20 changes: 10 additions & 10 deletions pyanalyze/test_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@


def test_is_compatible() -> None:
assert not is_compatible(List[int], 42)
assert is_compatible(List[int], [])
assert not is_compatible(List[int], ["x"])
assert is_compatible(List[int], [1, 2, 3])
assert not is_compatible(42, List[int])
assert is_compatible([], List[int])
assert not is_compatible(["x"], List[int])
assert is_compatible([1, 2, 3], List[int])


def test_get_compatibility_error() -> None:
assert (
get_compatibility_error(List[int], 42) == "Cannot assign Literal[42] to list\n"
get_compatibility_error(42, List[int]) == "Cannot assign Literal[42] to list\n"
)
assert get_compatibility_error(List[int], []) is None
assert get_compatibility_error([], List[int]) is None
assert (
get_compatibility_error(List[int], ["x"])
get_compatibility_error(["x"], List[int])
== "In element 0\n Cannot assign Literal['x'] to int\n"
)
assert get_compatibility_error(List[int], [1, 2, 3]) is None
assert get_compatibility_error([1, 2, 3], List[int]) is None


class TestRuntimeTypeGuard(TestNameCheckVisitorBase):
Expand All @@ -39,9 +39,9 @@ def want_lowercase(s: IsLower) -> None:

def capybara(s: str) -> None:
want_lowercase(s) # E: incompatible_argument
if is_compatible(IsLower, s):
if is_compatible(s, IsLower):
want_lowercase(s)

def asserting_capybara(s: str) -> None:
assert is_compatible(IsLower, s)
assert is_compatible(s, IsLower)
want_lowercase(s)
Loading