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

Fixes type narrowing for overlaping runtime types #11273

Merged
merged 3 commits into from
Oct 31, 2021
Merged

Fixes type narrowing for overlaping runtime types #11273

merged 3 commits into from
Oct 31, 2021

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Oct 5, 2021

As always, fixing issues with type narrowing / inference is always very hard, because small changes can backfire.
But, this looks as a simple change. Let's see how tests will behave.

Closes #11272

@github-actions

This comment has been minimized.

@sobolevn
Copy link
Member Author

sobolevn commented Oct 5, 2021

pydantic/utils.py:554: error: has no attribute "class" [attr-defined]

I am not sure what to do with this one. Maybe I should create a PR for pydantic with proper assert_never helper here?

src/websockets/legacy/server.py:364: error: unused "type: ignore" comment

Looks like we fixed a typing bug here 🎉
Repro:

from typing import Optional, Callable, Awaitable

async def some(x: Optional[Callable[[], Awaitable[str]]]) -> str:
    if x is not None:
        response = x()
        if isinstance(response, Awaitable):
            reveal_type(response)  # N: Revealed type is "typing.Awaitable[builtins.str]"
            return await response
        else:
            reveal_type(response)  # N: Revealed type is "<nothing>"
            return response

pandas/core/indexers/utils.py:360: error: has no attribute "stop" [attr-defined]

Cannot reproduce this one for now 🤔

@github-actions
Copy link
Contributor

github-actions bot commented Oct 5, 2021

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

pydantic (https://github.com/samuelcolvin/pydantic.git)
+ pydantic/utils.py:554: error: <nothing> has no attribute "__class__"  [attr-defined]

pandas (https://github.com/pandas-dev/pandas.git)
+ pandas/core/indexers/utils.py:360: error: <nothing> has no attribute "stop"  [attr-defined]
+ pandas/core/indexers/utils.py:360: error: <nothing> has no attribute "start"  [attr-defined]
+ pandas/core/indexers/utils.py:360: error: <nothing> has no attribute "step"  [attr-defined]
+ pandas/core/common.py:131: error: <nothing> has no attribute "dtype"  [attr-defined]

websockets (https://github.com/aaugustin/websockets.git)
+ src/websockets/legacy/server.py:364: error: unused "type: ignore" comment
+ src/websockets/legacy/server.py:592: error: unused "type: ignore" comment

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Thanks for the fix!

@hauntsaninja hauntsaninja merged commit f4a21a4 into python:master Oct 31, 2021
JukkaL pushed a commit that referenced this pull request Jan 7, 2022
JukkaL pushed a commit that referenced this pull request Jan 7, 2022
tushar-deepsource pushed a commit to DeepSourceCorp/mypy that referenced this pull request Jan 20, 2022
tushar-deepsource pushed a commit to DeepSourceCorp/mypy that referenced this pull request Jan 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Type narrowing of a union type works incorrectly in presence of a list
2 participants