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

Error for call to Any #688

Merged
merged 1 commit 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
3 changes: 2 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

## Unreleased

- Show an error on calls to `typing.Any` (#688)
- Add command-line option `-c`/`--code` to typecheck code from
the command line (#685)
- Add a `pyanalyze.extensions.EnumName` predicate and infer it
as the value for the `.name` attribute on enums. Also fix
type inference for enum "properties" on Python 3.11 and up. (#682)
- Allow `pyanalyze.runtime.is_compatible` to be used to narrow
types (#681)
types (#681, #687)
- Fix usage of `assert_type()` with `Any` and with unions of
`Annotated` objects (#680)
- Support inferring `MinLen` and `MaxLen` annotations based
Expand Down
15 changes: 15 additions & 0 deletions pyanalyze/implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1426,6 +1426,12 @@ def _bool_impl(ctx: CallContext) -> Value:
return annotate_with_constraint(TypedValue(bool), constraint)


# Any has a __call__ method at runtime that always raises.
def _any_impl(ctx: CallContext) -> Value:
ctx.show_error("Any is not callable. Maybe you meant cast(Any, ...)?")
return AnyValue(AnySource.error)


_POS_ONLY = ParameterKind.POSITIONAL_ONLY
_ENCODING_PARAMETER = SigParameter(
"encoding", annotation=TypedValue(str), default=KnownValue("")
Expand Down Expand Up @@ -1888,6 +1894,15 @@ def get_default_argspecs() -> Dict[object, Signature]:
impl=_bool_impl,
return_annotation=TypedValue(bool),
),
Signature.make(
[
SigParameter("args", ParameterKind.VAR_POSITIONAL),
SigParameter("kwargs", ParameterKind.VAR_KEYWORD),
],
callable=typing.Any,
impl=_any_impl,
return_annotation=AnyValue(AnySource.error),
),
# Typeshed has it as TypeGuard[Callable[..., object]], which causes some
# false positives.
Signature.make(
Expand Down
9 changes: 9 additions & 0 deletions pyanalyze/test_implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1345,3 +1345,12 @@ def capybara(x: object, unannotated, explicit_any: Any) -> None:
assert_type(explicit_any, Any)

assert_type(x, int) # E: inference_failure


class TestAny(TestNameCheckVisitorBase):
@assert_passes()
def test_call(self):
from typing import Any

def capybara():
Any(42) # E: incompatible_call
Loading