Skip to content

The type of class objects with defaults #18573

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
jfulbricht opened this issue Jan 30, 2025 · 1 comment
Closed

The type of class objects with defaults #18573

jfulbricht opened this issue Jan 30, 2025 · 1 comment
Labels
bug mypy got something wrong

Comments

@jfulbricht
Copy link

Bug Report

Annotating a generic function which accepts a class name as one of its arguments produces a false negative if that argument has a default.

To Reproduce

class User:
    # Defines fields like name, email

class BasicUser(User):
    def upgrade(self):
        """Upgrade to Pro"""

class ProUser(User):
    def pay(self):
        """Pay bill"""

def new_user[U: User](user_class: type[U] = User) -> U: # Error reported here.
    user = user_class()
    return user

Expected Behavior

No error should be reported.

Actual Behavior

Mypy reports Incompatible default for argument "user_class" (default has type "type[User]", argument has type "type[U]").

The use of the new generic syntax is irrelevant., i.e. the following

from typing import TypeVar

U = TypeVar('U', bound=User)

def new_user(user_class: type[U] = User) -> U:
    ...

produces the same error.

Your Environment

  • Mypy version used: 1.11.2
  • Mypy command-line flags: --enable-incomplete-feature=NewGenericSyntax
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.12
@jfulbricht jfulbricht added the bug mypy got something wrong label Jan 30, 2025
@A5rocks
Copy link
Collaborator

A5rocks commented Jan 30, 2025

Duplicate of #3737

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

No branches or pull requests

2 participants