forallpeople icon indicating copy to clipboard operation
forallpeople copied to clipboard

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

Open michaellisitsa opened this issue 3 years ago • 3 comments

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 or kN
2 * u.m
  • Following problem occurs on type checker: "m" is not a known member of module

michaellisitsa avatar Jan 17 '21 10:01 michaellisitsa

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 avatar Jan 28 '21 06:01 connorferster

@michaellisitsa

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

connorferster avatar Aug 31 '22 05:08 connorferster

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

drewcassidy avatar Oct 07 '22 04:10 drewcassidy