Skip to content

Conversation

alpaylan
Copy link
Contributor

@alpaylan alpaylan commented Jun 13, 2025

Summary

This PR fixes a false type inference issue specified in astral-sh/ty#649

If there is a better solution, I'm open to implementing that too. The current version uses the right-hand-side type when it finds a bool & NominalInstance pair, which is an improvement over the previous version, but I'm guessing there are bugs introduces by this too. I will keep inspecting those as I wait for the reviews.

Closes astral-sh/ty#649

Test Plan

This PR was tested through a regression test, added as an inline test to the module.

…e issue that coerces the inferred type to 'int' when the right side is a nominal instance such as a function call
@MichaReiser MichaReiser changed the title fix 'BitwiseAnd' bug at https://github.com/astral-sh/ty/issues/649 [ty] fix 'BitwiseAnd' bug Jun 13, 2025
@MichaReiser MichaReiser added the ty Multi-file analysis & type inference label Jun 13, 2025
carljm
carljm previously requested changes Jun 13, 2025
Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

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

Thanks for identifying the bug and proposing a fix! I think we want to adjust a few things in this PR, but it's a great start.

Copy link
Contributor

github-actions bot commented Jun 13, 2025

mypy_primer results

Changes were detected when running on open source projects
optuna (https://github.com/optuna/optuna)
- error[invalid-argument-type] optuna/visualization/_pareto_front.py:327:9: Argument is incorrect: Expected `bool`, found `int`
- Found 1005 diagnostics
+ Found 1004 diagnostics

yarl (https://github.com/aio-libs/yarl)
- error[invalid-assignment] yarl/_url.py:1019:13: Object of type `int` is not assignable to `bool`
- Found 159 diagnostics
+ Found 158 diagnostics

mypy (https://github.com/python/mypy)
- error[invalid-return-type] mypy/inspections.py:427:16: Return type does not match returned value: expected `tuple[dict[FuncBase | SymbolNode, State], bool]`, found `tuple[dict[Unknown, Unknown], int | @Todo(map_with_boundness: intersections with negative contributions)]`
- Found 2023 diagnostics
+ Found 2022 diagnostics

dd-trace-py (https://github.com/DataDog/dd-trace-py)
- error[invalid-return-type] ddtrace/appsec/_ddwaf/waf.py:143:16: Return type does not match returned value: expected `bool`, found `int`
- Found 6897 diagnostics
+ Found 6896 diagnostics

scipy (https://github.com/scipy/scipy)
- error[unresolved-attribute] scipy/stats/_mstats_basic.py:1919:7: Type `int` has no attribute `filled`
+ error[unresolved-attribute] scipy/stats/_mstats_basic.py:1919:7: Type `bool` has no attribute `filled`

… change the inference logic to be more general
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

Thanks!

…ol' in order to trigger regression properly, change the integer casting logic to cast when the operator is not 'BitOr | BitAnd | BitXor' or when the other operand is an 'IntLiteral'
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

Looks great, thank you!

@AlexWaygood AlexWaygood dismissed carljm’s stale review June 17, 2025 16:51

requested changes were made

@AlexWaygood AlexWaygood merged commit 932f941 into astral-sh:main Jun 17, 2025
35 checks passed
@AlexWaygood AlexWaygood changed the title [ty] fix 'BitwiseAnd' bug [ty] fix binary expression inference between boolean literals and bool instances Jun 17, 2025
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.

Bitwise operations between bool and Literal[True] are inferred as int instead of bool
4 participants