镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

[mypyc] Fix segfault from annotations naming classes that don't exist… - #22129

Merged
p-sawicki merged 1 commit into
python:masterfrom
rheard:fix-mypyc-1209
Oct 7, 2026
Merged

p-sawicki merged 1 commit into
python:masterfrom
rheard:fix-mypyc-1209

Conversation

@rheard

@rheard rheard commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Fixes mypyc/mypyc#1209.

mypyc rebuilds __annotations__ for dataclasses, attrs classes and non-native classes. For an annotation naming a compiled class, it loads that class's type object, but a compiled class doesn't exist until its class statement has run, so the type object can still be NULL. Storing the NULL in __annotations__ segfaulted at import. This happens when:

  • the class is in a module that hasn't been imported yet, for example because it's only imported under TYPE_CHECKING (the case in the issue), or
  • a non-native class names itself, or a class defined after it in the same module.

Now mypyc checks for NULL and stores the class's full name as a string instead, as it already does for a forward reference to a class later in the same module. With from __future__ import annotations, Python doesn't evaluate these annotations either. If the class does exist (for example because its module was imported first), the annotation is still the class itself.

@rheard

rheard commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor Author

@JukkaL Sorry for all the PRs but I use mypyc for a couple projects of mine, and they seem to be good test benches. I found a number of bugs when writing them that I've been fixing, and also I'm trying to squeeze as much performance as possible by looking for inefficiencies.

Definitely feel free to take your time with them. If there is anything I can do to help or make the process easier, let me know. Also I generally tried to keep my changes small and targeted, though some of them kind of got out of hand... so if you think any should be replaced with deeper structural fixes, also let me know and we can close them.

Sorry again, and thanks!

@p-sawicki p-sawicki left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, thanks!

@p-sawicki
p-sawicki merged commit 75e370b into python:master Oct 7, 2026
18 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.

Segfault importing compiled dataclass module when field annotation references TYPE_CHECKING-only compiled class

2 participants