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

False positive for type argument with default value #16795

Closed
alexei opened this issue Jan 18, 2024 · 1 comment
Closed

False positive for type argument with default value #16795

alexei opened this issue Jan 18, 2024 · 1 comment
Labels
bug mypy got something wrong

Comments

@alexei
Copy link

alexei commented Jan 18, 2024

Bug Report

mypy doesn't seem to like it when a type argument has a default value. I'm not sure how to explain this, so better see below.

To Reproduce

from typing import TypeVar

T = TypeVar("T", bound="Foo")

class Foo:
    pass

class SubFoo(Foo):
    pass

def factory(class_type: type[T]) -> T:
    return class_type()

factory(Foo)
factory(SubFoo)

def alt_factory(class_type: type[T] = Foo) -> T:  # ⚠️ Incompatible default for argument
    return class_type()

alt_factory()
alt_factory(Foo)
alt_factory(SubFoo)

https://mypy-play.net/?mypy=1.8.0&python=3.12&gist=804d71b3f77280d09347b352f535def4

Expected Behavior

I'd expect it to report no issues.

Actual Behavior

Incompatible default for argument "class_type" (default has type "type[Foo]", argument has type "type[T]")

Your Environment

mypy 1.8.0 on Python 3.12

@alexei alexei added the bug mypy got something wrong label Jan 18, 2024
@hauntsaninja
Copy link
Collaborator

Duplicate of #3737

@hauntsaninja hauntsaninja marked this as a duplicate of #3737 Jan 21, 2024
@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Jan 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants