-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Do not handle NotImplementedType as Any anymore.
#20114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
4143846
39d56bf
9bb029f
df6418d
fb04bad
9b584b6
0aa07ea
a9c6318
0fc0efc
c04458c
d3957d0
aed1acd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| From 9b584b626fc18881055a3f48080fe3afcf9bb583 Mon Sep 17 00:00:00 2001 | ||
| From: Christoph Tyralla <c.tyralla@bjoernsen.de> | ||
| Date: Sat, 25 Oct 2025 09:39:03 +0200 | ||
| Subject: [PATCH] modify typeshed instead of hacking `calculate_mro` | ||
|
|
||
| --- | ||
| mypy/typeshed/stdlib/builtins.pyi | 2 +- | ||
| 1 file changed, 1 insertion(+), 1 deletion(-) | ||
|
|
||
| diff --git a/mypy/typeshed/stdlib/builtins.pyi b/mypy/typeshed/stdlib/builtins.pyi | ||
| index ddf81db18..933a06640 100644 | ||
| --- a/mypy/typeshed/stdlib/builtins.pyi | ||
| +++ b/mypy/typeshed/stdlib/builtins.pyi | ||
| @@ -1269,7 +1269,7 @@ class property: | ||
|
|
||
| @final | ||
| @type_check_only | ||
| -class _NotImplementedType(Any): | ||
| +class _NotImplementedType: | ||
| __call__: None | ||
|
|
||
| NotImplemented: _NotImplementedType | ||
| -- | ||
| 2.45.1.windows.1 | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6852,3 +6852,96 @@ if isinstance(headers, dict): | |
|
|
||
| reveal_type(headers) # N: Revealed type is "Union[__main__.Headers, typing.Iterable[tuple[builtins.bytes, builtins.bytes]]]" | ||
| [builtins fixtures/isinstancelist.pyi] | ||
|
|
||
| [case testReturnNotImplementedInBinaryMagicMethods] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you move this to check-overloading? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests were originally in the "Returning Any" section of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd probably put them in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it is. But we may add many explicit overloads to them later... |
||
| # flags: --warn-return-any | ||
| from typing import Union | ||
|
|
||
| class A: | ||
| def __add__(self, other: object) -> int: | ||
| return NotImplemented | ||
| def __radd__(self, other: object) -> Union[int, NotImplementedType]: | ||
| return NotImplemented | ||
| def __sub__(self, other: object) -> Union[int, NotImplementedType]: | ||
| return 1 | ||
| def __isub__(self, other: object) -> int: | ||
| x: Union[int, NotImplementedType] | ||
| return x | ||
| def __mul__(self, other: object) -> Union[int, NotImplementedType]: | ||
| x: Union[int, NotImplementedType] | ||
| return x | ||
| [builtins fixtures/notimplemented.pyi] | ||
|
|
||
| [case testReturnNotImplementedABCSubclassHookMethod] | ||
| # flags: --warn-return-any | ||
| class A: | ||
| @classmethod | ||
| def __subclasshook__(cls, t: type[object], /) -> bool: | ||
| return NotImplemented | ||
| [builtins fixtures/notimplemented.pyi] | ||
|
|
||
| [case testReturnNotImplementedInNormalMethods] | ||
| # flags: --warn-return-any | ||
| from typing import Union | ||
|
|
||
| class A: | ||
| def f(self) -> bool: return NotImplemented # E: Incompatible return value type (got "_NotImplementedType", expected "bool") | ||
| def g(self) -> NotImplementedType: return True # E: Incompatible return value type (got "bool", expected "_NotImplementedType") | ||
| def h(self) -> NotImplementedType: return NotImplemented | ||
| def i(self) -> Union[bool, NotImplementedType]: return NotImplemented | ||
| def j(self) -> Union[bool, NotImplementedType]: return True | ||
| [builtins fixtures/notimplemented.pyi] | ||
|
|
||
| [case testNotImplementedReturnedFromBinaryMagicMethod] | ||
| # flags: --warn-return-any | ||
| from typing import Union | ||
|
|
||
| class A: | ||
| def __add__(self, x: A) -> Union[int, NotImplementedType]: ... | ||
| def __sub__(self, x: A) -> NotImplementedType: ... | ||
| def __imul__(self, x: A) -> Union[A, NotImplementedType]: ... | ||
| def __itruediv__(self, x: A) -> Union[A, NotImplementedType]: ... | ||
| def __ifloordiv__(self, x: A) -> Union[int, NotImplementedType]: ... | ||
| def __eq__(self, x: object) -> Union[bool, NotImplementedType]: ... | ||
| def __le__(self, x: int) -> Union[bool, NotImplementedType]: ... | ||
| def __lt__(self, x: int) -> NotImplementedType: ... | ||
| def __and__(self, x: object) -> NotImplementedType: ... | ||
| class B(A): | ||
| def __radd__(self, x: A) -> Union[int, NotImplementedType]: ... | ||
| def __rsub__(self, x: A) -> NotImplementedType: ... | ||
| def __itruediv__(self, x: A) -> Union[A, NotImplementedType]: ... | ||
| def __ror__(self, x: object) -> NotImplementedType: ... | ||
|
|
||
| a: A | ||
| b: B | ||
|
|
||
| reveal_type(a.__add__(a)) # N: Revealed type is "Union[builtins.int, builtins._NotImplementedType]" | ||
| reveal_type(a.__sub__(a)) # N: Revealed type is "builtins._NotImplementedType" | ||
| reveal_type(a.__imul__(a)) # N: Revealed type is "Union[__main__.A, builtins._NotImplementedType]" | ||
| reveal_type(a.__eq__(a)) # N: Revealed type is "Union[builtins.bool, builtins._NotImplementedType]" | ||
| reveal_type(a.__le__(1)) # N: Revealed type is "Union[builtins.bool, builtins._NotImplementedType]" | ||
|
|
||
| reveal_type(a + a) # N: Revealed type is "builtins.int" | ||
| reveal_type(a - a) # N: Revealed type is "Any" | ||
| reveal_type(a + b) # N: Revealed type is "builtins.int" | ||
| reveal_type(a - b) # N: Revealed type is "Any" | ||
| def f1(a: A) -> None: | ||
| a += a # E: Incompatible types in assignment (expression has type "int", variable has type "A") | ||
| def f2(a: A) -> None: | ||
| a -= a | ||
| reveal_type(a) # N: Revealed type is "__main__.A" | ||
| def f3(a: A) -> None: | ||
| a *= a | ||
| reveal_type(a) # N: Revealed type is "__main__.A" | ||
| def f4(a: A) -> None: | ||
| a /= a | ||
| reveal_type(a) # N: Revealed type is "__main__.A" | ||
| def f5(a: A) -> None: | ||
| a //= a # E: Result type of // incompatible in assignment | ||
| reveal_type(a == a) # N: Revealed type is "builtins.bool" | ||
| reveal_type(a == 1) # N: Revealed type is "builtins.bool" | ||
| reveal_type(a <= 1) # N: Revealed type is "builtins.bool" | ||
| reveal_type(a < 1) # N: Revealed type is "Any" | ||
| reveal_type(a and int()) # N: Revealed type is "Union[__main__.A, builtins.int]" | ||
| reveal_type(int() or a) # N: Revealed type is "Union[builtins.int, __main__.A]" | ||
| [builtins fixtures/notimplemented.pyi] | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...wait, but you still need a patch, right? (misc/typeshed_patches directory seems to be where they live) Our typeshed updates routine (misc/sync-typeshed.py) is "clone the HEAD of current typeshed, then apply patches in order", so your change will be lost during the next sync
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had a short look at it, and you are likely right. I have to admit that I am absolutely unfamiliar with the workflow and not very interested in spending time learning it. Would you like to contribute this change here (or in a separate PR - I do not know...)? Otherwise, I would simply go back to the original solution.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not deeply familiar with that either, but... Just
git format-patch -1 -o misc/typeshed_patches/ 9b584b mypy/typeshed/should do the trick (rename and edit the Subject line of the new file if you wish, commit&push) - I can open a PR with that file in your fork, but might be easier to just generate it on your end? (I didn't run the command, but the patches all look likeformat-patchoutput, and I'm moderately certain that I remember its arguments correctly)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, thanks a lot for your help; it seems to have worked exactly as you suggested!