Skip to content

Narrow TypeVar to bounded TypeVar #15377

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

Closed

Conversation

ikonst
Copy link
Contributor

@ikonst ikonst commented Jun 6, 2023

Fixes #15151.

When isinstance narrows a TypeVar, it should narrow it to a bounded TypeVar rather than to an Instance, so it could keep functioning as that TypeVar.

@github-actions

This comment has been minimized.

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Jun 6, 2023

I think I've broken mypy primer, will fix

@hauntsaninja hauntsaninja reopened this Jun 6, 2023
@github-actions

This comment has been minimized.

@hauntsaninja hauntsaninja reopened this Jun 6, 2023
@github-actions

This comment has been minimized.

@tmke8
Copy link
Contributor

tmke8 commented Jun 6, 2023

This is a great improvement!

There are still a few things that don't work, like this one:

# flags: --warn-unreachable
from typing import Optional, TypeVar

T = TypeVar("T", bound=Optional[int])  # `Union[None, int]` gives the same result

def foo(v: T) -> T:
    if v is None:
        return v  # E: Statement is unreachable  [unreachable]
    return v

but that's maybe a different problem.

@ikonst
Copy link
Contributor Author

ikonst commented Jun 6, 2023

Might be nice to somehow indicate the updated upper bound in the repr

@ikonst
Copy link
Contributor Author

ikonst commented Jun 6, 2023

I've added testIsinstanceAndNarrowTypeVariableIntersection (see 61b71e9) that's currently expected to fail: perhaps same root cause as the failure to narrow optional.

@ikonst
Copy link
Contributor Author

ikonst commented Jun 6, 2023

Surprisingly all mypy_primer results are expected :)

I'm making this do slightly more than originally planned by changing how narrowed TypeVars are stringified.

@ikonst ikonst force-pushed the 06-05-Narrow_TypeVar_to_bounded_TypeVar branch from f195e01 to 61b71e9 Compare June 6, 2023 15:04
@github-actions

This comment has been minimized.

@ikonst ikonst force-pushed the 06-05-Narrow_TypeVar_to_bounded_TypeVar branch from 61b71e9 to b84e3f8 Compare June 6, 2023 15:52
@github-actions

This comment has been minimized.

@ikonst
Copy link
Contributor Author

ikonst commented Jun 7, 2023

@JelleZijlstra This implements the first option I've outlined in #15151 (comment), can take a look?

@JelleZijlstra
Copy link
Member

I think I'll pass on this one, sorry. I'm mostly focusing my open-source time on projects other than mypy.

@ikonst
Copy link
Contributor Author

ikonst commented Jun 12, 2023

@ilevkivskyi What do you think?

Copy link
Contributor

github-actions bot commented Jan 4, 2024

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

mitmproxy (https://github.com/mitmproxy/mitmproxy)
+ mitmproxy/coretypes/serializable.py:186: error: Unused "type: ignore" comment  [unused-ignore]

steam.py (https://github.com/Gobot1234/steam.py)
- steam/ext/commands/commands.py:287: error: Argument 1 to "append" of "list" has incompatible type "Command[Any, Any, Any]"; expected "Self"  [arg-type]
- steam/ext/commands/commands.py:940: error: Incompatible return value type (got "Command[Any, Any, Any] | MaybeCommandT", expected "MaybeBool | MaybeCommandT")  [return-value]

mypy (https://github.com/python/mypy)
+ mypy/typeops.py:342: error: Incompatible types in assignment (expression has type "CallableType", variable has type "F")  [assignment]

pandas-stubs (https://github.com/pandas-dev/pandas-stubs)
+ tests/__init__.py:30: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/__init__.py:44: error: Unused "type: ignore" comment  [unused-ignore]

@ilevkivskyi
Copy link
Member

I propose to close this in favor of #19183 that offers a different approach.

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 'TypeVar' causes '[return-value]' error
5 participants