ivankorobkov / python-inject

Python dependency injection
Apache License 2.0
694 stars 79 forks source link

AttributeError: type object 'Callable' has no attribute '_abc_registry' #34

Closed xuleijian closed 5 years ago

xuleijian commented 5 years ago

When I use the lastest version Inject==3.5.2,I got the error "AttributeError: type object 'Callable' has no attribute '_abc_registry' ". But when I installed the lower version Inject==3.5.1,all the things are OK.Could you give me some help? Thank you !

ivankorobkov commented 5 years ago

Hi!

1) Which python version do you use? 2) Please, post the stacktrace and the code to reproduce the issue.

xuleijian commented 5 years ago

Thank you for your reply ! I use version 3.7 !

ERROR: Failure: AttributeError (type object 'Callable' has no attribute '_abc_registry')

Traceback (most recent call last): File "/usr/local/lib/python3.7/site-packages/nose/failure.py", line 39, in runTest raise self.exc_val.with_traceback(self.tb) File "/usr/local/lib/python3.7/site-packages/nose/loader.py", line 418, in loadTestsFromName addr.filename, addr.module) File "/usr/local/lib/python3.7/site-packages/nose/importer.py", line 47, in importFromPath return self.importFromDir(dir_path, fqname) File "/usr/local/lib/python3.7/site-packages/nose/importer.py", line 94, in importFromDir mod = load_module(part_fqname, fh, filename, desc) File "/usr/local/lib/python3.7/imp.py", line 234, in load_module return load_source(name, filename, file) File "/usr/local/lib/python3.7/imp.py", line 171, in load_source module = _load(spec) File "", line 696, in _load File "", line 677, in _load_unlocked File "", line 728, in exec_module File "", line 219, in _call_with_frames_removed File "/opt/atlassian/pipelines/agent/build/functions/test/unit/test_sel_handover/test_sel_handover.py", line 8, in from functions.src.api.handover import sel_handover File "/opt/atlassian/pipelines/agent/build/functions/src/api/handover/sel_handover.py", line 7, in from lib import inject File "/opt/atlassian/pipelines/agent/build/lib/inject.py", line 86, in from typing import Optional, Type, Hashable, Callable, TypeVar, Union File "/opt/atlassian/pipelines/agent/build/lib/typing.py", line 1357, in class Callable(extra=collections_abc.Callable, metaclass=CallableMeta): File "/opt/atlassian/pipelines/agent/build/lib/typing.py", line 1005, in new self._abc_registry = extra._abc_registry AttributeError: type object 'Callable' has no attribute '_abc_registry'

ivankorobkov commented 5 years ago

OK, I'll take a look.

xuleijian commented 5 years ago

@ivankorobkov Any solution find out ?

ivankorobkov commented 5 years ago

It seems the problem is with the typing dependency in some environments. I could not reproduce it, will have to look more into int.

jdmeyer3 commented 5 years ago

I am also having this issue and I think I've figured out what is happening. There are a couple of factors to this issue, first typing was introduced to the stdlib of Python since 3.5. The upstream package of typing is for versions older than 3.5. I believe that Python 3.7 is completely incompatible with this upstream package. Secondly, this is normally not an issue with typical imports as the Python3.7 interpreter usually prioritizes the stdlib over the virtualenv. But, when a system prioritizes the virtualenv over the stdlib, the incompatible typing package is loaded and breaks. To reproduce a system that would do this run the following

  1. Create a python3 virtualenv virtualenv -p python3.7 venv

  2. Source the venv

  3. Install inject pip3 install inject

  4. Create a module with the following code. Replace the sys.path with your interpreter paths

    
    import sys
    sys.path = [
    '<path to newly created venv>/venv/lib/python3.7/site-packages', 
    '/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7'] # This is my stdlib for my system (on my mac), replace this path with your python3.7 lib path

import inject


6. Run the python module

`python3.7 <your file>`

5. It should then error out with a message like

import inject Traceback (most recent call last): File "", line 1, in File "/private/tmp/test/venv/lib/python3.7/site-packages/inject.py", line 86, in from typing import Optional, Type, Hashable, Callable, TypeVar, Union File "/private/tmp/test/venv/lib/python3.7/site-packages/typing.py", line 1357, in class Callable(extra=collections_abc.Callable, metaclass=CallableMeta): File "/private/tmp/test/venv/lib/python3.7/site-packages/typing.py", line 1005, in new self._abc_registry = extra._abc_registry AttributeError: type object 'Callable' has no attribute '_abc_registry'

I would recommend making the typing package requirement only necessary for Python3.4 and below

ivankorobkov commented 5 years ago

That's a very helpful comment. Thanks a lot for it and for the pull request.

ivankorobkov commented 5 years ago

I have released version 3.5.3 with the fix. @xuleijian please, check. https://pypi.org/project/Inject/