connorferster / forallpeople

Python SI units library: your 'daily driver' for calculations.
Apache License 2.0
304 stars 40 forks source link

"m" is not a known member of module - Type Hinting of dynamically set units #22

Open michaellisitsa opened 3 years ago

michaellisitsa commented 3 years ago

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

connorferster commented 3 years ago

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...

connorferster commented 2 years ago

@michaellisitsa

I think this may have been fixed in VS Code. Have you noticed a change?

drewcassidy commented 2 years ago

it may have been fixed in vs code, but this issue persists with pycharm