Skip to content

An instance of a final class should be treated as satisfying a TypeVar bound to it, after being narrowed #19255

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

Open
A5rocks opened this issue Jun 8, 2025 · 0 comments
Labels
bug mypy got something wrong topic-type-narrowing Conditional type narrowing / binder topic-type-variables

Comments

@A5rocks
Copy link
Collaborator

A5rocks commented Jun 8, 2025

Bug Report

See title, I think this is the cause of the remaining issue for #9424. I think this is a followup to #19183.

To Reproduce

from typing import TypeVar, final

@final
class A:
    pass

U = TypeVar('U', bound=A | int)

def f(u: U) -> U:
    if isinstance(u, A):
        return A()
    else:
        return u

Expected Behavior

No errors

Actual Behavior

There's an error:

main.py:11: error: Incompatible return value type (got "A", expected "U")  [return-value]
Found 1 error in 1 file (checked 1 source file)

Your Environment

Reproduced on mypy play.

  • Mypy version used: v1.16
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.12
@A5rocks A5rocks added bug mypy got something wrong topic-type-variables topic-type-narrowing Conditional type narrowing / binder labels Jun 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-type-narrowing Conditional type narrowing / binder topic-type-variables
Projects
None yet
Development

No branches or pull requests

1 participant