Skip to content

[mypyc] Fix undeclared static for local Final with same name as module - #22369

Merged
JukkaL merged 1 commit into
masterfrom
mypyc-bulkfix-1152-local-final-module-name
Nov 4, 2026
Merged

JukkaL merged 1 commit into
masterfrom
mypyc-bulkfix-1152-local-final-module-name

Conversation

@JukkaL

@JukkaL JukkaL commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator

Fixes mypyc/mypyc#1152.

A reference to a local Final variable generated invalid C if a module with the same name existed (such as types or os):

def f(items: list[str]) -> dict[str, int]:
    types: Final = {i: len(i) for i in items}
    return dict(types)  # error: 'CPyStatic_types___' undeclared

The name of the variable was treated as a module-level final name, since it looked like a fully qualified name with a module prefix. Now only a literal final value is used directly for local final variables, and otherwise they are read like other local variables.

… module

Fixes mypyc/mypyc#1152.

A reference to a local `Final` variable generated invalid C if a module
with the same name existed (such as `types` or `os`):

```python
def f(items: list[str]) -> dict[str, int]:
    types: Final = {i: len(i) for i in items}
    return dict(types)  # error: 'CPyStatic_types___' undeclared
```

The name of the variable was treated as a module-level final name, since
it looked like a fully qualified name with a module prefix. Now only a
literal final value is used directly for local final variables, and
otherwise they are read like other local variables.
@JukkaL
JukkaL merged commit 7d251bf into master Nov 4, 2026
18 checks passed
@JukkaL
JukkaL deleted the mypyc-bulkfix-1152-local-final-module-name branch November 4, 2026 16:58
@mr-c

mr-c commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Thank you @JukkaL @p-sawicki !

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.

Final usage leading to gcc errror: use of a variable before declaration

3 participants