by wodenokoto · 6y ago · 0 comments
When you think about why that is, it makes sense, but I still find it annoying that while my type hints are not enforced, if I type-hint to an object that doesn't exist, I still get a run-time error.
I kinda wish that these hints would just be interpreted as comments during runtime.
You can do something like this:
from __future__ import annotations
Class Node:
parent: Optional[Node]
[1] https://www.python.org/dev/peps/pep-0563/The objects are not type-hinted; variables are. A variable in Python will always have a value (because it's just a label on a value, not a container), and that value will always be an object, and objects in Python always have a type.
If you mean that you are passing a variable that can have a value of None, this is what Optional[<type>] is for... Or maybe I'm missing something -- can you give an example?
import numpy as np
def f(x:numpy.array):
pass
Since numpy doesn’t exist in my namespace I get a runtime error (NameError) for a part of my code that supposedly isn’t used.I just feel like this should throw errors when running mypy and not when running my script.
[0] https://docs.python.org/3/whatsnew/3.7.html#pep-563-postpone...