pylint-dev / astroid

A common base representation of python source code for pylint and other projects
https://pylint.readthedocs.io/projects/astroid/en/latest/
GNU Lesser General Public License v2.1
531 stars 276 forks source link

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

Open DetachHead opened 2 years ago

DetachHead commented 2 years ago

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:

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

DanielNoord commented 2 years ago

@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..