Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Narrow falsey str/bytes/int to literal type#17818

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation

@brianschubert
Copy link
Collaborator

Closes#16891

Before

fromtypingimportLiteraldeff1(a:str)->Literal[""]:returnaandexit()# E: Incompatible return value type (got "str", expected "Literal['']")deff2(a:int)->Literal[0]:returnaandexit()# E: Incompatible return value type (got "int", expected "Literal[0]")deff3(a:bytes)->Literal[b""]:returnaandexit()# E: Incompatible return value type (got "bytes", expected "Literal[b'']")

After

Success: no issues found in 1 source file

@brianschubert
Copy link
CollaboratorAuthor

This is consistent with how pyright applies narrowing:Pyright playground

@github-actions

This comment has been minimized.

@TeamSpen210
Copy link
Contributor

Looked through the primer results:

  • urllib3 andhomeassistant: False positive, caused by accessing an attribute twice, but the attribute got modified in-between by other code. Kind of a general issue with narrowing, and having@property with side-effects.
  • packaging: True positive, detects the redundant check inif a followed byif not a and b.
  • prefect: Fixes an existing true positive, code treats"" differently to other strings and mypy is now able to understand how strings get narrowed out.
  • kornia: Same asprefect but with0 instead.
brianschubert reacted with thumbs up emoji

@brianschubert
Copy link
CollaboratorAuthor

Thanks for the analysis! I agree with your assessment.

Re: the false positives fromurllib3 andhomeassistant, this behavior is at least consistent with pyright, which also assumes that the type of a property doesn't change between evaluations:Pyright playground

@github-actions
Copy link
Contributor

Diff frommypy_primer, showing the effect of this PR on open source code:

urllib3 (https://github.com/urllib3/urllib3)+ test/test_response.py:179: error: Non-overlapping equality check (left operand type: "Literal[b'', ''] | None", right operand type: "Literal[b'foo']")  [comparison-overlap]packaging (https://github.com/pypa/packaging)+ src/packaging/version.py:496: error: Left operand of "and" is always true  [redundant-expr]core (https://github.com/home-assistant/core)+ homeassistant/components/vlc_telnet/media_player.py:181: error: Right operand of "and" is never evaluated  [unreachable]+ homeassistant/components/vlc_telnet/media_player.py:184: error: Statement is unreachable  [unreachable]prefect (https://github.com/PrefectHQ/prefect)- src/prefect/_internal/schemas/validators.py:820: error: Incompatible return value type (got "str", expected "Path")  [return-value]+ src/prefect/_internal/schemas/validators.py:820: error: Incompatible return value type (got "Literal['']", expected "Path")  [return-value]- src/prefect/deployments/runner.py:912: error: Item "str" of "str | DockerImage" has no attribute "reference"  [union-attr]- src/prefect/deployments/runner.py:917: error: Item "str" of "str | DockerImage" has no attribute "build"  [union-attr]- src/prefect/deployments/runner.py:921: error: Item "str" of "str | DockerImage" has no attribute "reference"  [union-attr]- src/prefect/deployments/runner.py:933: error: Item "str" of "str | DockerImage" has no attribute "push"  [union-attr]- src/prefect/deployments/runner.py:937: error: Item "str" of "str | DockerImage" has no attribute "reference"  [union-attr]- src/prefect/deployments/runner.py:941: error: Item "str" of "str | DockerImage" has no attribute "reference"  [union-attr]kornia (https://github.com/kornia/kornia)+ kornia/contrib/extract_patches.py:390: error: Unused "type: ignore" comment  [unused-ignore]+ kornia/contrib/extract_patches.py:395: error: Unused "type: ignore" comment  [unused-ignore]

Copy link
Collaborator

@hauntsaninjahauntsaninja left a comment

Choose a reason for hiding this comment

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

Nice, thank you!

brianschubert reacted with heart emoji
@hauntsaninjahauntsaninja merged commitf6520c8 intopython:masterSep 26, 2024
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@hauntsaninjahauntsaninjahauntsaninja approved these changes

Assignees

No one assigned

Labels

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

(🎁) narrow falsey string toLiteral[""]

3 participants

@brianschubert@TeamSpen210@hauntsaninja

[8]ページ先頭

©2009-2025 Movatter.jp