Repository navigation
Conversation
| class A: | ||
| def __new__(cls) -> NoReturn: ... | ||
|
|
||
| class B: | ||
| def __init__(self) -> NoReturn: ... | ||
|
|
||
| class C: | ||
| def __new__(cls) -> "C": ... | ||
| def __init__(self) -> NoReturn: ... | ||
|
|
||
| class D: | ||
| def __new__(cls) -> NoReturn: ... | ||
| def __init__(self) -> NoReturn: ... |
There was a problem hiding this comment.
class E:
def __new__(cls) -> NoReturn: ...
def __init__(self) -> None: ...Notable in it's absence is the example above. Unfortunately this example fails to properly recognize NoReturn when instantiating the class, as mypy tends to ignore __new__ if __init__ is defined:
Lines 969 to 973 in 8e82171
|
Any idea what's going on with the failed |
|
@BvB93 try to close / open this PR. It should be fixed now 👍 |
|
Let's give it a shot (note: I might need approval again as a first-time contributor) |
|
@BvB93 sorry, I cannot approve it 😞 |
|
I pushed the approval button. |
|
So, are there any further comments on this PR? |
|
Today I discovered that I can't annotate a class as unable to be instantiated through |
|
Please let me get something straight. So this PR proposes to be able to annotate the return type of a class' So like class MyExampleClass:
def __init__(self) -> NoReturn:
passrather than class MyExampleClass:
def __init__(self) -> None:
passIf that's the case, I am very much welcoming the change. It is more understandable to read that a class' |
42ea2e6 to
47df7ef
Compare
|
Closing and re-opening as no CI runs appear to be starting. |
Right, that's better. This PR does still need approval after the latest
@PedanticHacker not quite, class MyExampleClass:
def __init__(self) -> NoReturn:
raise TypeError("Cannot initialize 'MyExampleClass'") |
Co-Authored-By: Nikita Sobolev <mail@sobolevn.me>
|
Rebased to get rid of another merge conflict. For future reference, is there a preference for either rebasing or updating from master when dealing with merge conflicts? |
|
Diff from mypy_primer, showing the effect of this PR on open source code: steam.py (https://github.com/Gobot1234/steam.py)
- steam/ext/commands/converters.py:494: error: "__new__" must return a class instance (got "NoReturn") [misc]
- steam/ext/commands/commands.py:224: error: The type "Type[Greedy[Any]]" is not generic and not indexable [misc]
+ steam/ext/commands/commands.py:224: error: Value of type "Callable[[VarArg(Any), KwArg(Any)], NoReturn]" is not indexable [index]
|
Merging is generally preferred. When you force-push, the review history for comments before the force-push gets lost in the UI, making it harder for reviewers to review previous changes. |
hauntsaninja
left a comment
There was a problem hiding this comment.
This looks great! If you fix the merge conflicts and ping me, I will merge :-)
|
Hi @BvB93 @hauntsaninja, I made another PR which merged master, maybe it's worth to have a look. |
|
This got merged in #13480 |
|
Thank you BvB93! |
Closes #10342
This PR allows
__init__and__new__methods to returnNoReturn, allowing for the annotation of classesthat cannot be directly initialized (e.g. those that are defined in C such as
types.GeneratorType).