3 ms·
I'm not sure what do you mean by "type-hint to an object that doesn't exist"? The objects are not type-hinted; variables are. A variable in Python will always
by BerislavLopac 6y ago
I'm not sure what do you mean by "type-hint to an object that doesn't exist"?
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?
- wodenokoto 6y agoimport 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.
- kjagiello 6y agohttps://www.python.org/dev/peps/pep-0563/ https://www.python.org/dev/peps/pep-0563/
- BerislavLopac 6y agoThis is exactly what happens starting with Python 3.7, which implements PEP 563 [0]; you just need to enable it by importing annotations from __future__. Starting from Python 3.10 that will be the default. [0] [0] https://docs.python.org/3/whatsnew/3.7.html#pep-563-postponed-evaluation-of-annotations https://docs.python.org/3/whatsnew/3.7.html#pep-563-postpone...