forallpeople
forallpeople copied to clipboard
"m" is not a known member of module - Type Hinting of dynamically set units
The units imported imported into builtins appear not to be recognised by mypy language server (used in Pylance extension in VS Code) and gives errors depending on the strictness of your type checking.
Given the dynamic setting of builtin functions from the json file, this may not be possible to fix, but thought maybe there is some fixes you have in mind.
At the moment I'm using
2 * u.m # type: ignore
but that is line by line so not very elegant. There is some discussion of using a __setattr__
dunder method on the below link that allows any inputs, but I don't quite understand if that would block any type hints whatsoever for the class its applied to for other methods like .environment()
or .to()
, which might defeat the whole purpose of having type hints. https://stackoverflow.com/questions/50889677/remove-error-from-mypy-for-attributes-set-dynamically-in-a-python-class
It appears the below inside environments.py is where the __setattr__
occurs depending on the physical_class
inputted e.g. u.environment("structural")
the units are read from the json files and pushed to builtins.
# Then the push
for var_name, physical in units_dict.items():
setattr(builtins, var_name, physical)
Because the code isn't evaluated until runtime, then Pylance would not know about what variables will be created.
How to reproduce
- Use Pylance language server for VS code
- import forallpeople
import forallpeople as u
u.environment('structural')
- Use any units such as
m
orkN
2 * u.m
- Following problem occurs on type checker:
"m" is not a known member of module
Hi @michaellisitsa,
Thank you for bringing this up and directing me to the stackoverflow link. This is a known issue (for me) with forallpeople. It looks like this can be fixed with the __set_attr__
thing. I think there is a way to make this work...
@michaellisitsa
I think this may have been fixed in VS Code. Have you noticed a change?
it may have been fixed in vs code, but this issue persists with pycharm