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

Improve for loop index variable type narrowing #18014

Merged
merged 2 commits into from
Oct 23, 2024

Conversation

cdce8p
Copy link
Collaborator

@cdce8p cdce8p commented Oct 22, 2024

Preserve the literal type of index expressions a bit longer (until the next assignment) to support TypedDict lookups.

from typing import TypedDict

class X(TypedDict):
    hourly: int
    daily: int

def func(x: X) -> None:
    for var in ("hourly", "daily"):
        print(x[var])

Closes #9230

This comment has been minimized.

@cdce8p
Copy link
Collaborator Author

cdce8p commented Oct 22, 2024

Mypy primer results

scrapy

+ 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 str or bool. The intention is probably to apply decode to extra bytes items. Annotating it would require PEP 728.

https://github.com/scrapy/scrapy/blob/b4bad97eae6bcce790a626d244c63589f4134408/scrapy/downloadermiddlewares/cookies.py#L141-L145

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test b = a followed by reveal_type(b).

Copy link
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]

@JukkaL JukkaL merged commit 9e68959 into python:master Oct 23, 2024
18 checks passed
@cdce8p cdce8p deleted the improve-index-typing branch October 23, 2024 11:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Looping through literals not typed correctly
2 participants