Skip to content

Fix typing of attrs.ib with converter and default - #1655

Open
Omarinho12345 wants to merge 1 commit into
python-attrs:mainfrom
Omarinho12345:fix/attr-ib-converter-default
Open

Omarinho12345 wants to merge 1 commit into
python-attrs:mainfrom
Omarinho12345:fix/attr-ib-converter-default

Conversation

@Omarinho12345

Copy link
Copy Markdown

Fixes #518.

The attr.ib() stubs inferred the attribute type from the default argument even when a converter was passed. Since the converter is applied to the default at runtime, attr.ib(default="1", converter=int) was rejected when assigned to an int attribute. Added an overload that infers the type from the converter's return type and accepts any default value in that case.

Verification:

  • mypy typing_tests plus all stub files: Success: no issues found in 12 source files.
  • pytest tests -q: 1407 passed, 4 skipped, 2 xfailed.
  • ruff check and ruff format --check on the changed files: passed.
  • The new ConvCDefault case in typing_tests/mypy.py fails before the change with Incompatible types in assignment (expression has type "str", variable has type "int") and passes after it.

This branch has not been deployed

No deployments
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.

Typing issue (stubs) for converter with default

1 participant