-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[red-knot] Assignability for subclasses of Any
and Unknown
#17557
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
Conversation
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This LGTM, though I'd also include Unknown
, and I think making some exceptions for @final
classes does make sense.
It's unrelated to your PR, but while we're here: I find the comment at the top of this is_assignable_to
method slightly confusing. I'm not sure what it's trying to say (or why); I'd be inclined to just delete it?
## Summary We currently emit a diagnostic for code like the following: ```py from typing import Any # error: Invalid class base with type `GenericAlias` (all bases must be a class, `Any`, `Unknown` or `Todo`) class C(tuple[Any, ...]): ... ``` The changeset here silences this diagnostic by recognizing instances of `GenericAlias` in `ClassBase::try_from_type`, and inferring a `@Todo` type for them. This is a change in preparation for #17557, because `C` previously had `Unknown` in its MRO … ```py reveal_type(C.__mro__) # tuple[Literal[C], Unknown, Literal[object]] ``` … which would cause us to think that `C` is assignable to everything. The changeset also removes some false positive `invalid-base` diagnostics across the ecosystem. ## Test Plan Updated Markdown tests.
79df46c
to
d457975
Compare
Any
and Unknown
With the new changes, we also allow subclasses of # This is a union with Unknown, because `IOError` is a
# simple `IOError = OSError` type alias in typeshed
reveal_type(IOError) # Unknown | Literal[OSError]
class MyError(IOError): ...
reveal_type(MyError.__mro__) # tuple[Literal[MyError], Unknown, Literal[object]]
raise MyError() # no invalid-raise diagnostic anymore we still raise "Invalid class base with type |
* main: (28 commits) [red-knot] Make `BoundMethodType` a salsa interned (#17581) [red-knot] Emit a diagnostic if a non-protocol is passed to `get_protocol_members` (#17551) [red-knot] Add more tests for protocol members (#17550) [red-knot] Assignability for subclasses of `Any` and `Unknown` (#17557) [red-knot] mypy_primer: add strawberry, print compilation errors to stderr (#17578) [red-knot] GenericAlias instances as a base class (#17575) Remove redundant `type_to_visitor_function` entries (#17564) Fixes how the checker visits `typing.cast`/`typing.NewType` arguments (#17538) [red-knot] Class literal `__new__` function callable subtyping (#17533) [red-knot] Surround intersections with `()` in potentially ambiguous contexts (#17568) [minor] Delete outdated TODO comment (#17565) [red-knot] add regression test for fixed cycle panic (#17535) [red-knot] fix unions of literals, again (#17534) red_knot_python_semantic: remove last vestige of old diagnostics! red_knot_python_semantic: migrate `types` to new diagnostics red_knot_python_semantic: migrate `types/diagnostic` to new diagnostics red_knot_python_semantic: migrate `types/call/bind` to new diagnostics red_knot_python_semantic: migrate `types/string_annotation` to new diagnostics red_knot_python_semantic: migrate `types/infer` to new diagnostic model red_knot_python_semantic: migrate INVALID_ASSIGNMENT for inference ...
…var-instance * dcreager/generic-constructor: (29 commits) We don't need this [red-knot] Make `BoundMethodType` a salsa interned (#17581) [red-knot] Emit a diagnostic if a non-protocol is passed to `get_protocol_members` (#17551) [red-knot] Add more tests for protocol members (#17550) [red-knot] Assignability for subclasses of `Any` and `Unknown` (#17557) [red-knot] mypy_primer: add strawberry, print compilation errors to stderr (#17578) [red-knot] GenericAlias instances as a base class (#17575) Remove redundant `type_to_visitor_function` entries (#17564) Fixes how the checker visits `typing.cast`/`typing.NewType` arguments (#17538) [red-knot] Class literal `__new__` function callable subtyping (#17533) [red-knot] Surround intersections with `()` in potentially ambiguous contexts (#17568) [minor] Delete outdated TODO comment (#17565) [red-knot] add regression test for fixed cycle panic (#17535) [red-knot] fix unions of literals, again (#17534) red_knot_python_semantic: remove last vestige of old diagnostics! red_knot_python_semantic: migrate `types` to new diagnostics red_knot_python_semantic: migrate `types/diagnostic` to new diagnostics red_knot_python_semantic: migrate `types/call/bind` to new diagnostics red_knot_python_semantic: migrate `types/string_annotation` to new diagnostics red_knot_python_semantic: migrate `types/infer` to new diagnostic model ...
Summary
Allow (instances of) subclasses of
Any
andUnknown
to be assignable to (instances of) other classes, unless they are final. This allows us to get rid of ~1000 false positives, mostly when mock-objects likeunittest.mock.MagicMock
are assigned to various targets.Test Plan
Adapted and new Markdown tests.