Skip to content

Python mypy errors when record member names match type names #2552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mhammond
Copy link
Member

pub struct Record {
    bool: bool, // sad mypy
}

cargo test -p uniffi-fixture-keywords-rust works, but:

% mypy target/tmp/uniffi_keywords_rust-xxx/keywords_rust.py
.../keywords_rust.py:1161: error: Variable "keywords_rust.RecordWithTypeNames.bool" is not valid as a type  [valid-type]

This just demonstrates the error but makes no attempt to fix it yet.

@mhammond mhammond requested a review from a team as a code owner May 30, 2025 03:36
@mhammond mhammond requested review from bendk and removed request for a team May 30, 2025 03:36
@mhammond
Copy link
Member Author

mhammond commented May 30, 2025

"Rust and Foreign Language tests" are failing with the mypy errors.

@mhammond mhammond marked this pull request as draft May 30, 2025 03:57
@mhammond mhammond force-pushed the push-wumupxmwkrxt branch from 9e23fb4 to 059c2bf Compare June 14, 2025 09:03
Before this, we'd see:

```
pub struct Record {
    bool: bool, // sad mypy
}
```

`cargo test -p uniffi-fixture-keywords-rust` works, but:

```
% mypy target/tmp/uniffi_keywords_rust-xxx/keywords_rust.py
.../keywords_rust.py:1161: error: Variable "keywords_rust.RecordWithTypeNames.bool" is not valid as a type  [valid-type]
```
@mhammond mhammond force-pushed the push-wumupxmwkrxt branch from 059c2bf to 4625463 Compare June 14, 2025 09:06
@mhammond
Copy link
Member Author

This isn't just builtins:

class foo: # unlikely, but looking at the general case...
    pass

class Test:
    bool: bool
    int: int
    foo: foo
    def __init__(self, bool: bool, foo: foo):
        pass

t = Test(True, foo())

mypy complains:

/tmp/t.py:8: error: Variable "t.Test.bool" is not valid as a type  [valid-type]
/tmp/t.py:8: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
/tmp/t.py:8: error: Variable "t.Test.foo" is not valid as a type  [valid-type]

(not clear why int isn't also an error)

So my initial naive idea of just special-casing builtin types and using a fully-qualified name from the builtins module isn't going to work in general (although may actually be fine in practice?) I can't see this addressed in the mypy docs.

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.

1 participant