Skip to content

[red-knot] Add assignability of function literals to callables#17095

Merged
dhruvmanila merged 4 commits intoastral-sh:mainfrom
MatthewMckee4:funcion-literal-assignability
Mar 31, 2025
Merged

[red-knot] Add assignability of function literals to callables#17095
dhruvmanila merged 4 commits intoastral-sh:mainfrom
MatthewMckee4:funcion-literal-assignability

Conversation

@MatthewMckee4
Copy link
Contributor

Summary

Part of #16953

Test Plan

Update is_assignable_to.md

@github-actions
Copy link
Contributor

github-actions bot commented Mar 31, 2025

mypy_primer results

Changes were detected when running on open source projects
pyinstrument (https://github.com/joerick/pyinstrument)
- error[lint:invalid-return-type] /tmp/mypy_primer/projects/pyinstrument/test/util.py:122:12: Object of type `Literal[wrapped]` is not assignable to return type `() -> None`
- error[lint:invalid-argument-type] /tmp/mypy_primer/projects/pyinstrument/test/low_level/test_context.py:48:19: Object of type `Literal[busy_wait]` cannot be assigned to parameter 2 (`callable`) of bound method `run`; expected type `(...) -> Unknown`
- error[lint:invalid-argument-type] /tmp/mypy_primer/projects/pyinstrument/test/low_level/test_context.py:49:19: Object of type `Literal[busy_wait]` cannot be assigned to parameter 2 (`callable`) of bound method `run`; expected type `(...) -> Unknown`
- Found 273 diagnostics
+ Found 270 diagnostics

isort (https://github.com/pycqa/isort)
- error[lint:invalid-return-type] /tmp/mypy_primer/projects/isort/isort/literal.py:84:12: Object of type `Literal[wrap]` is not assignable to return type `((Any, ISortPrettyPrinter, /) -> str, /) -> (Any, ISortPrettyPrinter, /) -> str`
- error[lint:invalid-argument-type] /tmp/mypy_primer/projects/isort/isort/main.py:1126:77: Object of type `Literal[_preconvert]` cannot be assigned to parameter `default` of function `dumps`; expected type `((Any, /) -> Any) | None`
- error[lint:invalid-assignment] /tmp/mypy_primer/projects/isort/isort/settings.py:749:9: Object of type `Literal[from_string]` is not assignable to `((str, /) -> Any) | type[Any]`
- Found 53 diagnostics
+ Found 50 diagnostics

black (https://github.com/psf/black)
- error[lint:invalid-return-type] /tmp/mypy_primer/projects/black/src/black/trans.py:2482:12: Object of type `Literal[insert_str_child]` is not assignable to return type `(@Todo(Inference of subscript on special form), /) -> None`
- Found 212 diagnostics
+ Found 211 diagnostics

rich (https://github.com/Textualize/rich)
- error[lint:invalid-parameter-default] /tmp/mypy_primer/projects/rich/rich/cells.py:51:25: Default value of type `Literal[cached_cell_len]` is not assignable to annotated parameter type `(str, /) -> int`
- error[lint:invalid-assignment] /tmp/mypy_primer/projects/rich/rich/pretty.py:226:9: Object of type `Literal[display_hook]` is not assignable to attribute `displayhook` of type `(object, /) -> Any`
- error[lint:invalid-return-type] /tmp/mypy_primer/projects/rich/rich/console.py:512:16: Object of type `Literal[_replace]` is not assignable to return type `(...) -> Group`
- error[lint:invalid-return-type] /tmp/mypy_primer/projects/rich/rich/console.py:514:12: Object of type `Literal[decorator]` is not assignable to return type `(...) -> (...) -> Group`
- error[lint:invalid-assignment] /tmp/mypy_primer/projects/rich/rich/traceback.py:207:9: Object of type `Literal[excepthook]` is not assignable to attribute `excepthook` of type `(type[BaseException], BaseException, TracebackType | None, /) -> Any`
- Found 583 diagnostics
+ Found 578 diagnostics

@dhruvmanila dhruvmanila added the ty Multi-file analysis & type inference label Mar 31, 2025
Copy link
Member

@dhruvmanila dhruvmanila left a comment

Choose a reason for hiding this comment

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

Looks good, thanks!

…es/is_assignable_to.md

Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com>
@dhruvmanila dhruvmanila enabled auto-merge (squash) March 31, 2025 15:39
@dhruvmanila dhruvmanila merged commit 4a6fa5f into astral-sh:main Mar 31, 2025
22 checks passed
@MatthewMckee4 MatthewMckee4 deleted the funcion-literal-assignability branch March 31, 2025 15:43
dhruvmanila added a commit that referenced this pull request Apr 16, 2025
## Summary

This is similar to #17095, it adds
assignability check for bound methods to callables.

## Test Plan

Add test cases to for assignability; specifically it uses gradual types
because otherwise it would just delegate to `is_subtype_of`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants