Skip to content

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Nov 15, 2021

Now this code typechecks with no problems:

class A:
    def __class_getitem__(cls, a: int) -> type[A]:
        return cls

A[1]

But, this code raises:

class A:
    def __class_getitem__(cls, a: str) -> type[A]:
        return cls

A[1]  # E: Argument 1 to "__class_getitem__" of "A" has incompatible type "int"; expected "str"

This is a WIP, because I want to know how many things will break because of this. I'm intersted in both primer and test outputs. I will add tests in next commits.

Closes #11501

@sobolevn
Copy link
Member Author

Oh wow, only 4 tests fail! 🎉

 =========================== short test summary info ============================
FAILED mypy/test/testcheck.py::TypeCheckSuite::testMetaclassGetitem
FAILED mypy/test/testcheck.py::TypeCheckSuite::testInvalidTypeApplicationType
FAILED mypy/test/testcheck.py::TypeCheckSuite::testTypeApplicationCrash
FAILED mypy/test/testcheck.py::TypeCheckSuite::testCallableParsingFromExpr

I can totally fix it! 👍

@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

schemathesis (https://github.com/schemathesis/schemathesis.git)
+ src/schemathesis/constants.py: note: In member "from_str" of class "CodeSampleStyle":
+ src/schemathesis/constants.py:69: error: "Type[CodeSampleStyle]" has no attribute "__class_getitem__"
+ src/schemathesis/cli/options.py: note: In member "convert" of class "CSVOption":
+ src/schemathesis/cli/options.py:31: error: "Type[Enum]" has no attribute "__class_getitem__"
- src/schemathesis/models.py: note: In member "from_requests" of class "Response":
- src/schemathesis/models.py:785: error: Unexpected keyword argument "status_code" for "Response"

@sobolevn
Copy link
Member Author

Based on primer output, we have a problem with Enum 🤔
Related line: https://github.com/schemathesis/schemathesis/blob/5a09a03d8558ca5cde1ef32b50290c1ba2949980/src/schemathesis/constants.py#L69

@sobolevn
Copy link
Member Author

sobolevn commented Nov 15, 2021

More problems:

class A:
    def __class_getitem__(cls, a: int) -> type[A]:
        return cls

class B:
    def __class_getitem__(cls, a: int) -> type[B]:
        return cls

x = A | B
x[2]
# out/ex.py:21: error: Bad number of arguments for type alias, expected: 0, given: 1
# out/ex.py:21: error: Type application is only supported for generic classes
# out/ex.py:21: error: Invalid type: try using Literal[2] instead?
class A:
    def __class_getitem__(cls, a: int) -> type[A]:
        return cls

class B:
    def __class_getitem__(cls, a: int) -> type[B]:
        return cls

x: type[A | B]
x[2]
# out/ex.py:21: error: Value of type "Union[Type[A], Type[B]]" is not indexable

@sobolevn
Copy link
Member Author

sobolevn commented Nov 18, 2021

I am going to change the scope of this PR: __class_getitem__ is going to be ignored for now (so, all will stay the same as it is now). I will only change how metaclasses with __getitem__ work.

Maybe one day I will return to __class_getitem__.

@sobolevn sobolevn marked this pull request as draft November 19, 2021 08:48
@sobolevn sobolevn closed this Feb 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mypy does not handle __class_getitem__

1 participant