ManderaGeneral / generalimport

Handle all your optional dependencies with a single call!
https://pypi.org/project/generalimport/
Apache License 2.0
16 stars 1 forks source link

Allow typing from missing dependency #31

Closed ZanSara closed 1 year ago

ZanSara commented 1 year ago

Fixes https://github.com/ManderaGeneral/generalimport/issues/30


After adding the unit tests, seems like __args__ is not sufficient in the general case. I'll keep investigating.

ZanSara commented 1 year ago

Alright so the real culprit, very unfortunately, seems to be __hash__. Or better, this is a typing optimization, because behind the scenes it uses functools.lru_cache to try caching types, but according to the docstring of lru_cache:

Arguments to the cached function must be hashable.

which FakeModule is not.

By making it hashable, typing with Optional and Union without issues. However, some tests fail:

FAILED generalimport/test/test_usage/test_cast.py::Test::test_hash - AssertionError: MissingOptionalDependency not raised
FAILED generalimport/test/test_usage/test_cast.py::Test::test_iter - AssertionError: MissingOptionalDependency not raised
FAILED generalimport/test/test_usage/test_compare.py::Test::test_eq - AssertionError: MissingOptionalDependency not raised

Do we want to trade these usecases for the possibility of typing?

Mandera commented 1 year ago

Good stuff! I built an experiment on top of this in #39 - possibly giving us best of both worlds

Action | Typing works | hash(x) and x == y triggers | ID -- | -- | -- | -- Current | false | true |   Make hashable | true | false | 31 Only hashable if typing is in callstack | true | true | 39