Allow returning Literals in __new__#15687
Conversation
This comment has been minimized.
This comment has been minimized.
sobolevn
left a comment
There was a problem hiding this comment.
Please, add tests to this change :)
This comment has been minimized.
This comment has been minimized.
sobolevn
left a comment
There was a problem hiding this comment.
Please, also add an @overload case you are proposing to typeshed
This comment has been minimized.
This comment has been minimized.
This reverts commit 79cb39e.
This comment has been minimized.
This comment has been minimized.
sobolevn
left a comment
There was a problem hiding this comment.
Let's ask for an extra pair of eyes.
@ilevkivskyi would you, please? :)
ilevkivskyi
left a comment
There was a problem hiding this comment.
Looks good, added a suggestion to improve the tests.
This comment has been minimized.
This comment has been minimized.
|
Somehow |
|
@Gobot1234 Yes, I think you likely need to add Lines 195 to 207 in cfd01d9 |
Thank you, never would have spotted that! Everything should work now |
This comment has been minimized.
This comment has been minimized.
|
FWIW you are doing something weird with the fixtures. Try placing your updated definitions of |
|
Like this? [case testOverride__new__WithLiteralReturnPassing]
from typing import Literal
class Falsy:
def __bool__(self) -> Literal[False]: pass
reveal_type(bool(Falsy())) # N: Revealed type is "Literal[False]"
reveal_type(int()) # N: Revealed type is "Literal[0]"
[file builtins.py]
from typing import Literal, Protocol, overload
class str: pass
class dict: pass
class float: pass
class int:
def __new__(cls) -> Literal[0]: pass
class _Truthy(Protocol):
def __bool__(self) -> Literal[True]: pass
class _Falsy(Protocol):
def __bool__(self) -> Literal[False]: pass
class bool(int):
@overload
def __new__(cls, __o: _Truthy) -> Literal[True]: pass
@overload
def __new__(cls, __o: _Falsy) -> Literal[False]: pass
def __new__(cls, __o: object):
pass
[typing fixtures/typing-medium.pyi]It doesn't seem to work |
|
No, move all that stuff from |
Doing so doesn't seem to have changed anything, sorry if I'm misunderstanding |
|
Hm, to debug this, try adding |
|
bool is Any |
This comment has been minimized.
This comment has been minimized.
|
Bumping because python/typeshed#6069 and python/typeshed#10465 depend on this. |
This comment has been minimized.
This comment has been minimized.
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
|
Thank you @ilevkivskyi for helping with this much appreciated! |


Unblocks python/typeshed#10465