Skip to content

Fix #20821: Check __qualname__ assignment type in class body#21211

Open
BHUVANSH855 wants to merge 5 commits intopython:masterfrom
BHUVANSH855:fix-20821-dunder-qualname
Open

Fix #20821: Check __qualname__ assignment type in class body#21211
BHUVANSH855 wants to merge 5 commits intopython:masterfrom
BHUVANSH855:fix-20821-dunder-qualname

Conversation

@BHUVANSH855
Copy link
Copy Markdown

Fixes #20821

This PR adds validation for assignments to __qualname__ inside class bodies.

Previously, mypy allowed assigning non-string values to __qualname__, even though this raises a TypeError at runtime in Python. This PR aligns mypy’s behavior with CPython by enforcing that __qualname__ must be a string.

Changes

  • Added a semantic analysis check for assignments to __qualname__ in class scope
  • Emit an error if the assigned value is not a string
  • Added a test case to cover this behavior

Example

class X:
    __qualname__ = 5  # Now correctly produces an error

Notes

  • The check is implemented in semantic analysis (semanal.py)
  • The change is minimal and only affects class-level assignments to __qualname__

@github-actions

This comment has been minimized.

@github-actions
Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

dd-trace-py (https://github.com/DataDog/dd-trace-py)
+ ddtrace/debugging/_function/discovery.py:46: error: "__qualname__" must be str  [misc]

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.

mypy should check qualname (and other special dunders)

1 participant