-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Fix TypeGuard Explicit Any Check #12050
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
base: master
Are you sure you want to change the base?
Fix TypeGuard Explicit Any Check #12050
Conversation
Hmm I don't think my current approach makes much sense for other type visitors. Delegating visitor to the inner type works for explicit any check, but x: T and x: TypeGuard[T] are very different with the latter not even being valid as TypeGuard only works as a return type. And the argument doesn't correspond to the actual return type. I'm currently thinking I should instead add a visit_type_guard function to TypeVisitor that other visitor classes would need to override to pick a reasonable way of handling typeguards. |
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.
Thanks!
@@ -171,7 +171,7 @@ def deserialize_type(data: Union[JsonDict, str]) -> 'Type': | |||
raise NotImplementedError('unexpected .class {}'.format(classname)) | |||
|
|||
|
|||
class Type(mypy.nodes.Context): | |||
class Type(mypy.nodes.Context, ABC): |
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 is not required, we can catch missing @abstractmethod
s with mypy
itself.
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.
Hmm, interesting this is one place mypy/pyright differs microsoft/pyright#2547. Pyright requires ABC as during runtime the error is only raised for ABC.
I'm fine dropping ABC, just wanted to note if you want run other type checkers on mypy's codebase this will make a difference.
@@ -43,7 +43,8 @@ class Iterator(Iterable[T_co], Protocol): | |||
def __next__(self) -> T_co: pass | |||
|
|||
class Sequence(Iterable[T_co]): | |||
def __getitem__(self, n: Any) -> T_co: pass | |||
# misc is for explicit Any. | |||
def __getitem__(self, n: Any) -> T_co: pass # type: ignore[misc] |
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.
n: int
?
Description
Fixes #12049. This pr is stacked on top of this pr. I would only review the last commit for this pr and ignored required portion of it.
This adds an explicit Any check for TypeGuard argument. This requires adding an accept method to TypeGuard. To avoid new type classes not defining accept I marked accept abstract method.
Two open questions,
Test Plan
I ran local tests and checked that my test case in the bug report is fixed. The error message produced for this code,
I'll add a test case after agreement this approach is reasonable.