Skip to content

AttributeError: __metadata__ on Python 3.13 #303

Description

@kamyarghajar

There is compatibility problem which raises the following exception:

File "venv/lib/python3.13/site-packages/injector/__init__.py", line 1768, in build
    return self._build_class(self._target, **kwargs)
           ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib/python3.13/site-packages/injector/__init__.py", line 1763, in _build_class
    return self._injector.create_object(cls, additional_kwargs=kwargs)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib/python3.13/site-packages/injector/__init__.py", line 1177, in create_object
    self.call_with_injection(init, self_=instance, kwargs=additional_kwargs)
    ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib/python3.13/site-packages/injector/__init__.py", line 1216, in call_with_injection
    bindings = get_bindings(callable)
  File "venv/lib/python3.13/site-packages/injector/__init__.py", line 1358, in get_bindings
    has_injectable_parameters = any(
        _is_specialization(v, Annotated) and _inject_marker in v.__metadata__
        for v in type_hints.values()
    )
  File "venv/lib/python3.13/site-packages/injector/__init__.py", line 1359, in <genexpr>
    _is_specialization(v, Annotated) and _inject_marker in v.__metadata__
                                                           ^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.13/3.13.11/Frameworks/Python.framework/Versions/3.13/lib/python3.13/typing.py", line 1378, in __getattr__
    raise AttributeError(attr)
AttributeError: __metadata__. Did you mean: '__getattr__'?

which might be fixed by replacing:

has_injectable_parameters = any(
    _is_specialization(v, Annotated) and _inject_marker in v.__metadata__
    for v in type_hints.values()
)

in __init__.py file with:

has_injectable_parameters = any(
    _is_specialization(v, Annotated) and _inject_marker in getattr(v, "__metadata__", ())
    for v in type_hints.values()
)

Activity

  1. kamyarghajar commented on Jan 8, 2026

    @kamyarghajar
    ContributorAuthor

    Pull request: #304

  2. kamyarghajar commented on Jan 8, 2026

    @kamyarghajar
    ContributorAuthor

    MRE

    import sys
    from dataclasses import dataclass
    from typing import Literal
    from injector import Injector
    
    # Trigger: Python 3.13 + Literal + slots=True
    # In Python 3.13, the internal representation of Literal can sometimes 
    # satisfy _is_specialization(v, Annotated) but lack the __metadata__ attribute.
    @dataclass(slots=True)
    class ServiceConfig:
        environment: Literal["prod", "test"] = "test"
    
    def reproduce():
        print(f"Python version: {sys.version}")
        injector = Injector()
        
        print("Attempting to create object (this triggers get_bindings)...")
        try:
            # This will trigger the AttributeError on Python 3.13 with injector <= 0.23.0
            injector.get(ServiceConfig)
            print("Success: No error raised.")
        except AttributeError as e:
            print(f"\nCRASH DETECTED: {e}")
            if "__metadata__" in str(e):
                print("Confirmed: This is the Python 3.13 __metadata__ regression.")
        except Exception as e:
            print(f"Unexpected error: {type(e).__name__}: {e}")
    
    if __name__ == "__main__":
        reproduce()
    
  3. changed the title [-]Compatibility with Python 3.13[/-] [+]AttributeError: __metadata__ on Python 3.13[/+] on Jan 8, 2026
  4. added a commit that references this issue on Jan 15, 2026
    333a0fa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions