Skip to content

Improve docs for UP046 regarding reusable TypeVars #18894

@wardVD

Description

@wardVD

Summary

The rule non-pep695-generic-class doesn't seem to cover a use case where you define a TypeVar in a module which is imported in several other files.

For example, imagine I have a file
my_types.py

with

from typing import TypeVar

CustomNumber = TypeVar("CustomNumber", bound=int)

and two files a.py with

from mytypes import CustomNumber

class A(Generic[CustomNumber]):
    ...

and b.py with

from mytypes import CustomNumber

class B(Generic[CustomNumber]):
    ...

If I want to change CustomNumber to have an upper bound of float I would only need to change in one location. If I would adhere to PEP695 I would need to remove the my_types.py file and refactor the code to e.g.

class A[CustomNumber: int]():
    ...

and

class B[CustomNumber: int]():
    ...

thus breaking code reproducibility if I would want to change the upper bound. This could lead to code inconsistencies

I deliberately made the example a bit dummy and trivial but I think it makes the issue clear :-).

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentationhelp wantedContributions especially welcome

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions