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 and still trigger by hash and eq #39

Closed Mandera closed 1 year ago

Mandera commented 1 year ago

Closes #30 Closes #31

An experiment, might work. The idea is to make FakeModule hashable only if typing module is in the call stack.

Possibly the best of both worlds. Questionable implementation. Clean up if proceeding!

ZanSara commented 1 year ago

Wow! This is some serious black magic, but if that gives us typing and triggers on __eq__ and __hash__ we should go for it! Thanks for the help on this issue :blush:

Mandera commented 1 year ago

Got a pretty clean implementation for the dynamic dunders now! I want to make the _inside_typing() function better, the venv structure is different depending on linux vs windows, it's clearly not a robust way to check if call stack goes through the typing module

Mandera commented 1 year ago

Perhaps something like inspect.getmodule(frame) and something from frame.f_globals to prevent false positive from a random package having a typing.py file

Mandera commented 1 year ago

Ohh!! Can just do

from inspect import getmodule
import typing

...
if getmodule(frame) is typing: