Improve for loop index variable type narrowing#18014
Merged
JukkaL merged 2 commits intopython:masterfrom Oct 23, 2024
Merged
Conversation
This comment has been minimized.
This comment has been minimized.
Collaborator
Author
Mypy primer resultsscrapy + scrapy/downloadermiddlewares/cookies.py:145: error: "str" has no attribute "decode"; maybe "encode"? [attr-defined]
+ scrapy/downloadermiddlewares/cookies.py:152: error: "str" has no attribute "decode"; maybe "encode"? [attr-defined]These are true positives. All TypedDict values |
JukkaL
approved these changes
Oct 23, 2024
Collaborator
JukkaL
left a comment
There was a problem hiding this comment.
Nice to see several now-unnecessary type ignores in mypy_primer output! Looks good, just one minor test-related suggestion.
| a = "yearly" | ||
| reveal_type(a) # N: Revealed type is "builtins.str" | ||
| a = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "str") | ||
| reveal_type(a) # N: Revealed type is "builtins.str" |
Collaborator
There was a problem hiding this comment.
Test b = a followed by reveal_type(b).
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: pydantic (https://github.com/pydantic/pydantic)
+ pydantic/v1/networks.py:345: error: Unused "type: ignore" comment [unused-ignore]
scrapy (https://github.com/scrapy/scrapy)
+ scrapy/downloadermiddlewares/cookies.py:141: error: Unused "type: ignore" comment [unused-ignore]
+ scrapy/downloadermiddlewares/cookies.py:142: error: Unused "type: ignore" comment [unused-ignore]
+ scrapy/downloadermiddlewares/cookies.py:145: error: Unused "type: ignore" comment [unused-ignore]
+ scrapy/downloadermiddlewares/cookies.py:145: error: "str" has no attribute "decode"; maybe "encode"? [attr-defined]
+ scrapy/downloadermiddlewares/cookies.py:145: note: Error code "attr-defined" not covered by "type: ignore" comment
+ scrapy/downloadermiddlewares/cookies.py:152: error: Unused "type: ignore" comment [unused-ignore]
+ scrapy/downloadermiddlewares/cookies.py:152: error: "str" has no attribute "decode"; maybe "encode"? [attr-defined]
+ scrapy/downloadermiddlewares/cookies.py:152: note: Error code "attr-defined" not covered by "type: ignore" comment
discord.py (https://github.com/Rapptz/discord.py)
- discord/message.py:2265: error: TypedDict key must be a string literal; expected one of ("channel_id", "guild_id", "id", "author", "content", ...) [literal-required]
core (https://github.com/home-assistant/core)
+ homeassistant/components/image_processing/__init__.py:226: error: Unused "type: ignore" comment [unused-ignore]
+ homeassistant/components/energy/data.py:334: error: Unused "type: ignore" comment [unused-ignore]
schemathesis (https://github.com/schemathesis/schemathesis)
+ src/schemathesis/cli/__init__.py:260: error: Unused "type: ignore" comment [unused-ignore]
|
19 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Preserve the literal type of index expressions a bit longer (until the next assignment) to support TypedDict lookups.
Closes #9230