None and non-type value as the type annotation #1317
dai-akita-7554
started this conversation in
General
Replies: 1 comment 4 replies
-
How should the interpreter detect whether something is a type or not? A type hint can look like This check isn't something that the typing community can do though. It would need to be done at cpython. But my experience with adding checks for common errors in cpython is that:
|
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have never used the type annotation, or typehinting, of python so that I could not understand why this line was run without syntax error:
At the line,
some_object.do_something()
is executed depending on where the line is in the code.It turned out that it seemed to be accidentally modified from
which did not make a type annotation but just utilized
if
statement to check None.I realized that the modified line was interpreted as a type annotation without assignment. Whether
some_object.do_something()
is executed depends on where the line is in the code, according to the issue of PEP526. In the section "Capturing the declared types at runtime" of the issue, a (bad) example where a function call is specified as the type is shown:(This results in not printing "Hello world")
PEP526 shows an example in which variables are type-annotated with strings:
and says
PEP526 also says that it rejected the proposal "Do not evaluate annotations, treat them as strings". While I guess the feature of the evaluation is necessary for generics or something, I have no idea to use values which are not type as the type annotation. It would be more useful to detect lines like
as syntax errors if
some_object.do_something()
is not a type. It would be also useful to detect errors whensome_object.do_something()
is None,I would appreciate it if you posted some comments on None and non-type value as the type annotation.
Beta Was this translation helpful? Give feedback.
All reactions