Skip to content

option to treat TYPE_CHECKING as True, or add a separate variable #1332

Open
@DetachHead

Description

@DetachHead

Current problem

i have some edge cases where a class behaves differently at runtime than what the type checker thinks. here's a super minimal example:

class Foo:
    if TYPE_CHECKING:
        def __init__(self) -> None:
            pass

    else:
        def __init__(self, value: int) -> None:
            pass


a = Foo() # pylint: No value for argument 'value' in constructor (no-value-for-parameter)

in this case i don't want a pylint error here for the same reason i don't want a mypy error.

Desired solution

either:

  • option to treat TYPE_CHECKING as True
  • add a PYLINT_LINTING variable that works the same way, which is treated as True by the linter and False at runtime

Additional context

i'm making a ReifiedGeneric class that returns a _ReifiedGenericAlias when __class_getitem__ is called at runtime:

class ReifiedGeneric(Generic[T], metaclass=_ReifiedGenericMetaclass):
    __orig_class__: OrigClass

    if not TYPE_CHECKING:
        def __class_getitem__(cls, item) -> type[ReifiedGeneric[T]]:
            generic_alias = super().__class_getitem__(item)
            return _ReifiedGenericAlias(
                generic_alias.__origin__, generic_alias.__args__
            )

long story short, pylint complains here when i don't want it to even know about this _ReifiedGenericAlias class:

class Reified(ReifiedGeneric[T]):
    def __init__(self) -> None: # __init__ method from base class '_ReifiedGenericAlias' is not called (super-init-not-called)
        print(1)

See:
pylint-dev/pylint#5637 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions