Skip to content

Fix Python 3.9: _get_class_annotations inherits parent annotations via getattr MRO traversal#18

Merged
harumaki4649 merged 2 commits intomainfrom
copilot/fix-ci-yml-python-error
Mar 8, 2026
Merged

Fix Python 3.9: _get_class_annotations inherits parent annotations via getattr MRO traversal#18
harumaki4649 merged 2 commits intomainfrom
copilot/fix-ci-yml-python-error

Conversation

Copy link
Contributor

Copilot AI commented Mar 8, 2026

In Python 3.9, getattr(cls, '__annotations__') traverses the MRO and returns a parent class's annotations dict when the child declares none. Python 3.10+ fixed this to return {} instead — causing _class_to_schema() to silently include inherited fields only on 3.9.

Changes

  • _get_class_annotations: Added an object-identity guard after the getattr fallback. If the returned mapping is the same object (is) as any base class's own __annotations__, it's MRO-inherited and we return {} instead.
  • Preserves the getattr fallback for metaclass-defined __annotations__ properties (lazily computed annotations), since those return a freshly constructed dict each time — never is-identical to a base class's annotations.
class Base:
    x: int

class Child(Base):
    role = v.str()  # no own annotations

schema = _class_to_schema(Child)
# Before (Python 3.9): {"role": ..., "x": ...}  ← 'x' leaked from Base
# After:               {"role": ...}             ← only own fields

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: harumaki4649 <83683593+harumaki4649@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix CI configuration for Python 3.9 errors Fix Python 3.9: _get_class_annotations inherits parent annotations via getattr MRO traversal Mar 8, 2026
@harumaki4649 harumaki4649 marked this pull request as ready for review March 8, 2026 14:16
@harumaki4649 harumaki4649 merged commit 6e4a5af into main Mar 8, 2026
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants