Skip to content

Fix crash introduced in Python 3.8 in _AttributeInjection. #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions inject/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,29 @@ def __call__(self) -> T:
return self._instance


class _NullInjectedAttribute(object):
"""
Returned for injected attributes that don't have an injection configuration.
"""
pass


class _AttributeInjection(object):
def __init__(self, cls):
self._cls = cls

def __get__(self, obj, owner):
# Some Python internals (starting in 3.8 specifically) may call
# getattr() for a class's attributes to do some reflective inspection.
# That triggers this codepath, potentially before inject.configure()
# has been called, which is why we should silently return a sentinel
# type representing a null value instead of raising.
try:
return instance(self._cls)
except InjectorException:
return _NullInjectedAttribute()


class _AttributeInjection(object):
Copy link

@dineshtrivedi dineshtrivedi Feb 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andrewborba10 I was curious to see your changes, and it made me wonder one thing.

You added a new _AttributeInjection class like below:

class _NullInjectedAttribute(object):
    """
    Returned for injected attributes that don't have an injection configuration.
    """
    pass


class _AttributeInjection(object):
    def __init__(self, cls):
        self._cls = cls

    def __get__(self, obj, owner):
        # Some Python internals (starting in 3.8 specifically) may call
        # getattr() for a class's attributes to do some reflective inspection.
        # That triggers this codepath, potentially before inject.configure()
        # has been called, which is why we should silently return a sentinel
        # type representing a null value instead of raising.
        try:
            return instance(self._cls)
        except InjectorException:
            return _NullInjectedAttribute()

It seems to me that now we should delete this _AttributeInjection class that does not return the sentinel value. And maybe we could also add the type hints to the new class.

Am I missing something?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was definitely unintentional, I meant to modify the existing class. It sounds like Ivan might have some other changes in mind, so I'll let him apply those with the fix to remove the old class.

def __init__(self, cls: Binding) -> None:
self._cls = cls
Expand Down