psf/requests · error · AttributeError

' ' object has no attribute

Error message

'{type(self).__name__}' object has no attribute '{key}'

What it means

Raised by LookupDict.__getattr__() when accessing an attribute that is not stored in the lookup. LookupDict is the backing class for requests.codes (the status code registry). Normal dict-style access (codes['key']) returns None for missing keys, but attribute access (codes.unknown_key) raises AttributeError.

Solutions

  1. Use the correct status code name — check dir(requests.codes) or the documentation for valid names.
  2. Use the integer status code directly (e.g. 200 instead of requests.codes.ok) for clarity and to avoid typos.
  3. Use bracket access codes['name'] which returns None instead of raising, if you want graceful miss handling.

Example fix

# before
if resp.status_code == requests.codes.not_found:  # AttributeError

# after
if resp.status_code == 404:
# or
if resp.status_code == requests.codes.notfound:
Defensive patterns

Strategy: try-catch

Validate before calling

valid_names = dir(requests.codes)
if code_name not in valid_names:
    raise ValueError(f'Unknown status code name: {code_name}')

Try / catch

try:
    code = getattr(requests.codes, name)
except AttributeError:
    code = None  # or fall back to integer

Prevention

When it happens

Trigger: Accessing requests.codes.nonexistent or requests.codes.WRONG_NAME where the name isn't registered in the _codes mapping; typos in status code names; using an outdated alias that was removed in a newer version.

Common situations: Typo in a status code name like requests.codes.okay instead of requests.codes.ok; referencing a non-standard code not in the registry; version differences where an alias was added or removed.

Related errors


AI-assisted analysis of psf/requests@8068356288 (2026-08-11). Data as JSON: /api/errors/671f252897e54f03. Report an issue: GitHub.

Appendix: source

Thrown at src/requests/structures.py:114

class LookupDict(dict[str, _VT]):
    """Dictionary lookup object."""

    name: Any

    def __init__(self, name: Any = None) -> None:
        self.name = name
        super().__init__()

    def __repr__(self) -> str:
        return f"<lookup '{self.name}'>"

    def __getattr__(self, key: str) -> _VT | None:
        # We need this for type checkers to infer typing
        # on attribute access with status_codes.py
        if key in self.__dict__:
            return self.__dict__[key]
        else:
            raise AttributeError(
                f"'{type(self).__name__}' object has no attribute '{key}'"
            )

    def __getitem__(self, key: str) -> _VT | None:  # type: ignore[override]
        # We allow fall-through here, so values default to None

        return self.__dict__.get(key, None)

    @overload
    def get(self, key: str, default: None = None) -> _VT | None: ...

    @overload
    def get(self, key: str, default: _D | _VT) -> _D | _VT: ...

    def get(self, key: str, default: _D | None = None) -> _VT | _D | None:
        return self.__dict__.get(key, default)

View on GitHub (pinned to 8068356288)