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
#4897: allow raise E
where E: Type[Exn]
#4918
Conversation
that is, where E is annotated as a Type, rather than being inferred as a FunctionLike which is a type object. This also allows `raise E` where E is Type[Any], which *seems* right but I was not certain about.
test-data/unit/check-statements.test
Outdated
[case testRaiseExceptionType] | ||
import typing | ||
x = None # type: typing.Type[BaseException] | ||
raise x |
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.
Could you also add a negative test (e.g., x: Type[int]; raise x
still errors)?
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.
done
I think allowing |
I think your code would probably be cleaner and start type-checking if you first checked if it's an instance of TypeType and extracted the inner type before doing the Basically, extract the inner type first if needed, then do the same checks whether the type was extracted or not. |
done |
ping? |
@@ -2357,6 +2357,10 @@ def visit_raise_stmt(self, s: RaiseStmt) -> None: | |||
def type_check_raise(self, e: Expression, s: RaiseStmt, | |||
optional: bool = False) -> None: | |||
typ = self.expr_checker.accept(e) | |||
if isinstance(typ, TypeType): |
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.
Shouldn't (for consistency) the same case be added to check_except_handler_test
(with test cases)?
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.
if you mean that this should work:
[case testExceptWithTypeType]
import typing
E = BaseException # type: typing.Type[BaseException]
try:
pass
except E:
pass
[builtins fixtures/exception.pyi]
it already does, though I can certainly add that test.
@bwo, do you have a chance to look at Ivan's feedback? |
@msullivan finally got back to this. Thanks for the reminder. |
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.
OK, looks good to me now!
That is, where E is annotated as a Type, rather than being inferred as a
FunctionLike which is a type object.
This also allows
raise E
where E is Type[Any], which seems right butI was not certain about.
Fixes #4897