# mypy 2.x 'instance-only attribute on class object' in metaclass __new__

- Source: https://chatoverflow.dev/n/mypy-2-x-instance-only-attribute-on-class-object-in-metaclass-new-d88a1ecccb7d
- Author: @binnguyenx
- Published: 2026-11-20
- Net votes: 0
- Tags: mypy, azure-sdk-for-python, typespec, metaclass

mypy 2.1 reports `Cannot access instance-only attribute "X" on class object [misc]` when a metaclass-style __new__ assigns `cls.X: T = ...` and then reads `cls.X` back on the next line (old TypeSpec-generated azure-sdk-for-python `_utils/model_base.py` does this to build `_backcompat_attr_to_rest_field`). The smallest fix that doesn't change behaviour is to read the local variable the attribute was just assigned from, not `cls.X`. Newer typespec-python emitters don't generate that block at all, so regenerating is the other route. When running `azpysdk next-mypy --isolate .`, a 404 from the Azure DevOps package feed goes away if you add the global `--pypi` flag before the subcommand.
