-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Stop TypedDictAnalyzer from leaking synthetic types #13732
Closed
Michael0x2a
wants to merge
6
commits into
python:master
from
Michael0x2a:fix-typeddict-semanal-analyzer-leaking-synthetic-types
Closed
Stop TypedDictAnalyzer from leaking synthetic types #13732
Michael0x2a
wants to merge
6
commits into
python:master
from
Michael0x2a:fix-typeddict-semanal-analyzer-leaking-synthetic-types
Commits on Oct 23, 2022
-
Stop TypedDictAnalyzer from leaking synthetic types
Fixes python#10007 Mypy currently crashes when you try: 1. Creating a class-based TypedDict containing a malformed type hint 2. Asking it to compute fine-grained dependencies, either via running dmypy or by setting the `--cache-fine-grained` flag. Here is the exact sequence of events that leads to this crash: 1. Since the type annotation is malformed, semanal initially gives the type annotation a type of `RawExpressionType`. 2. TypedDictAnalyzer (correctly) determines determines that the type of the malformed type annotation should be treated as just `Any` in: https://github.com/python/mypy/blob/f5ce4ee6ca7e2d8bb1cde8a2b49865f53bbacff5/mypy/semanal_typeddict.py#L289 3. TypedDictAnalyzer forgets to modify `stmt.type` like we normally do after calling `self.anal_type` in normal classes: https://github.com/python/mypy/blob/f5ce4ee6ca7e2d8bb1cde8a2b49865f53bbacff5/mypy/semanal.py#L3022 4. Mypy _does_ use the `Any` type when constructing the TypeInfo for the TypedDict. This is why mypy will not crash under most conditions: the correct type is being used in most places. 5. Setting `--cache-fine-grained` will make mypy perform one final pass against the AST to compute fine-grained dependencies. As a part of this process, it traverses the AssigmentStatement's `type` field using TypeTriggersVisitor. 6. TypeTriggersVisitor is _not_ a SyntheticTypeVisitor. So, the visitor trips an assert when we try traversing into the RawExpressionType. Interestingly, this same crash does not occur for NamedTuples despite the fact that NamedTupleAnalyzer also does not set `stmt.type`: https://github.com/python/mypy/blob/f5ce4ee6ca7e2d8bb1cde8a2b49865f53bbacff5/mypy/semanal_namedtuple.py#L177 It turns out this is because semanal.py will end up calling the `analyze_class_body_common(...)` function after NamedTupleAnalyzer runs, but _not_ after TypedDictAnalyzer runs: - https://github.com/python/mypy/blob/f5ce4ee6ca7e2d8bb1cde8a2b49865f53bbacff5/mypy/semanal.py#L1510 - https://github.com/python/mypy/blob/f5ce4ee6ca7e2d8bb1cde8a2b49865f53bbacff5/mypy/semanal.py#L1479 I'm not sure why this is: ideally, the two analyzers ought to have as similar semantics as possible. But refactoring this felt potentially disruptive, so I went for the narrower route of just patching TypedDictAnalyzer.
Configuration menu - View commit details
-
Copy full SHA for ef4d492 - Browse repository at this point
Copy the full SHA ef4d492View commit details -
Configuration menu - View commit details
-
Copy full SHA for 1f9a1c0 - Browse repository at this point
Copy the full SHA 1f9a1c0View commit details
Commits on Oct 24, 2022
-
Configuration menu - View commit details
-
Copy full SHA for 6956bf0 - Browse repository at this point
Copy the full SHA 6956bf0View commit details -
Configuration menu - View commit details
-
Copy full SHA for bcc768c - Browse repository at this point
Copy the full SHA bcc768cView commit details
Commits on Nov 14, 2022
-
Configuration menu - View commit details
-
Copy full SHA for dd5913a - Browse repository at this point
Copy the full SHA dd5913aView commit details
Commits on Dec 3, 2022
-
Configuration menu - View commit details
-
Copy full SHA for 8aa9fd0 - Browse repository at this point
Copy the full SHA 8aa9fd0View commit details
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.