fix str.join with and/or expression narrowing #2875#2906
fix str.join with and/or expression narrowing #2875#2906asukaminato0721 wants to merge 1 commit intofacebook:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes a Pyrefly-specific narrowing issue where and/or boolean expressions involving type | None could leave an imprecise type that then broke overload resolution for str.join (issue #2875).
Changes:
- Generalize boolop result-type narrowing by reusing
atomic_narrow(..., IsTruthy/IsFalsy)for intermediate operands. - Make
Type::Type(_)statically truthy inType::as_bool()to support correctand/ornarrowing for class objects. - Add a regression test ensuring
sorted((e and e.__name__) or "None" ...)produceslist[str]and is accepted by", ".join(...).
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
pyrefly/lib/test/literal.rs |
Adds regression test covering str.join with and/or narrowing over `type |
pyrefly/lib/alt/narrow.rs |
Exposes atomic_narrow to other alt modules (pub(crate)) so boolop inference can reuse it. |
pyrefly/lib/alt/expr.rs |
Updates boolop inference to narrow intermediate results via atomic_narrow(IsTruthy/IsFalsy) instead of bespoke literal cases. |
crates/pyrefly_types/src/types.rs |
Treats Type::Type(_) as statically truthy for as_bool(), enabling the intended narrowing behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Diff from mypy_primer, showing the effect of this PR on open source code: steam.py (https://github.com/Gobot1234/steam.py)
- ERROR steam/ext/commands/commands.py:826:11-25: `type[Command] | type[C]` is not assignable to variable `cls` with type `type[C]` [bad-assignment]
|
Primer Diff Classification✅ 1 improvement(s) | 1 project(s) total | -1 errors 1 improvement(s) across steam.py.
Detailed analysis✅ Improvement (1)steam.py (-1)
Was this helpful? React with 👍 or 👎 Classification by primer-classifier (1 LLM) |
Summary
Fixes #2875
boolean-expression inference now keeps the truthiness-refined slice of a non-final operand instead of the full operand type, which stops
(e and e.__name__)or "None" from leakingtype[Any]into the result.Test Plan
add test