Skip to content

The type of class objects with defaults #18573

Closed as not planned
Closed as not planned
@jfulbricht

Description

@jfulbricht

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions