{"id":"671f252897e54f03","repo":"psf/requests","slug":"type-self-name-object-has-no-attribute","errorCode":null,"errorMessage":"'{type(self).__name__}' object has no attribute '{key}'","messagePattern":"'(.+?)' object has no attribute '(.+?)'","errorType":"exception","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"src/requests/structures.py","lineNumber":114,"sourceCode":"class LookupDict(dict[str, _VT]):\n    \"\"\"Dictionary lookup object.\"\"\"\n\n    name: Any\n\n    def __init__(self, name: Any = None) -> None:\n        self.name = name\n        super().__init__()\n\n    def __repr__(self) -> str:\n        return f\"<lookup '{self.name}'>\"\n\n    def __getattr__(self, key: str) -> _VT | None:\n        # We need this for type checkers to infer typing\n        # on attribute access with status_codes.py\n        if key in self.__dict__:\n            return self.__dict__[key]\n        else:\n            raise AttributeError(\n                f\"'{type(self).__name__}' object has no attribute '{key}'\"\n            )\n\n    def __getitem__(self, key: str) -> _VT | None:  # type: ignore[override]\n        # We allow fall-through here, so values default to None\n\n        return self.__dict__.get(key, None)\n\n    @overload\n    def get(self, key: str, default: None = None) -> _VT | None: ...\n\n    @overload\n    def get(self, key: str, default: _D | _VT) -> _D | _VT: ...\n\n    def get(self, key: str, default: _D | None = None) -> _VT | _D | None:\n        return self.__dict__.get(key, default)\n","sourceCodeStart":96,"sourceCodeEnd":131,"githubUrl":"https://github.com/psf/requests/blob/414f0513c33883adf6f2b46901d4f0b38a455851/src/requests/structures.py#L96-L131","documentation":"requests.structures.LookupDict (used for requests.codes, the status-code lookup) overrides __getattr__ to serve attribute access from the instance __dict__ and raise a standard-style AttributeError for anything not present. So requests.codes.ok works, but a misspelled or nonexistent code name raises this error. Note __getitem__ deliberately falls through to None instead, so bracket access never raises — an intentional asymmetry.","triggerScenarios":"Accessing an attribute that isn't a registered status-code name on requests.codes (e.g. requests.codes.okay, requests.codes.not_found_ with a typo), or attribute access on any other LookupDict instance for a key never inserted. Raised at src/requests/structures.py:110-116.","commonSituations":"Typos in status-code names (requests.codes.no_found); guessing names that aren't in the alias table; code written against dict-style access assuming attribute access has the same return-None behavior as lookup['missing']; static-analysis-invisible dynamic attributes making these errors appear only at runtime.","solutions":["Fix the attribute name — check valid aliases in requests.status_codes (e.g. requests.codes.ok, requests.codes.not_found, requests.codes.teapot)","Compare against integer literals or http.HTTPStatus instead: response.status_code == 404","If a soft lookup is intended, use bracket access (requests.codes['maybe_missing'] returns None) or getattr with a default"],"exampleFix":"# before\nif r.status_code == requests.codes.no_found:  # AttributeError\n    ...\n\n# after\nif r.status_code == requests.codes.not_found:\n    ...","handlingStrategy":"try-catch","validationCode":"key = \"Content-Type\"\nvalue = headers.get(key)  # returns None instead of raising","typeGuard":"def has_header(mapping, key):\n    return key in mapping  # CaseInsensitiveDict membership is case-insensitive","tryCatchPattern":"try:\n    value = obj.some_attr\nexcept AttributeError:\n    value = None  # attribute-style access on requests structures raises AttributeError for unknown keys","preventionTips":["Access headers via dict-style headers.get('Name') rather than attribute-style access","Rely on 'key in response.headers' membership tests before indexing","Remember header lookups are case-insensitive, but attribute access on these objects is not a supported API"],"tags":["python","attribute-error","status-codes","api-usage"],"analyzedSha":"414f0513c33883adf6f2b46901d4f0b38a455851","analyzedAt":"2026-07-31T19:24:57.696Z","schemaVersion":2}