首页 > 解决方案 > PyCharm Asks "Is this intentional?" When Passing the Proper Arguments to Constructor of Generic Class

问题描述

In the following case, PyCharm 2020.2.3 (Professional Edition) Build #PY-202.7660.27 complains about me passing an int to the constructor of the class (it underlines the 2):

Passing int instead of [...].GenClass. Is this intentional?

from typing import Generic, TypeVar


A = TypeVar("A")


class GenClass(Generic[A]):
    def __init__(self, p: int):
        self.p = p


if __name__ == "__main__":
    a = GenClass[str](2)

I'm using Python 3.9. The fact that it expects a GenClass object seems to me like PyCharm wants the self object from the constructor. That looks like an error but maybe I'm using generics the wrong way.

Could you please give me advice on whether the error is with me or with PyCharm? Thanks a lot!

标签: pythongenericsconstructorpycharmtype-hinting

解决方案


推荐阅读