astroid icon indicating copy to clipboard operation
astroid copied to clipboard

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

Open DetachHead opened this issue 3 years ago • 1 comments

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: https://github.com/PyCQA/pylint/issues/5637#issuecomment-1006162401

DetachHead avatar Jan 06 '22 00:01 DetachHead

@DetachHead Please use the issue template when filing issues, even if you have opened the issue before. Issues like this are not likely to be taken up by anyone..

DanielNoord avatar Jan 06 '22 08:01 DanielNoord