fix Python interpreter gets error, using [int] with a function call while pyrefly doesn't get error #2590#2598
fix Python interpreter gets error, using [int] with a function call while pyrefly doesn't get error #2590#2598asukaminato0721 wants to merge 1 commit intofacebook:mainfrom
Conversation
|
Diff from mypy_primer, showing the effect of this PR on open source code: mitmproxy (https://github.com/mitmproxy/mitmproxy)
- ERROR test/mitmproxy/proxy/layers/http/test_http3.py:89:41-66: Expected a type form, got instance of `(cls: type[bytes] = ...) -> _Placeholder[bytes] | bytes` [not-a-type]
- ERROR test/mitmproxy/proxy/layers/http/test_http3.py:91:35-67: `|` is not supported between `(cls: type[bytes] = ...) -> _Placeholder[bytes] | bytes` and `None` [unsupported-operation]
+ ERROR test/mitmproxy/proxy/layers/http/test_http3.py:89:41-66: `Placeholder` is not subscriptable [unsupported-operation]
+ ERROR test/mitmproxy/proxy/layers/http/test_http3.py:91:35-60: `Placeholder` is not subscriptable [unsupported-operation]
|
Primer Diff Classification✅ 1 improvement(s) | 1 project(s) total 1 improvement(s) across mitmproxy.
Detailed analysis✅ Improvement (1)mitmproxy (+2, -2)
Was this helpful? React with 👍 or 👎 Classification by primer-classifier (1 LLM) |
There was a problem hiding this comment.
Pull request overview
Fixes a mismatch between CPython runtime behavior and pyrefly when subscripting generic functions/callables (e.g., func[int](...)), making pyrefly report an error and adding a regression test for issue #2590.
Changes:
- Block subscripting
Type::Forallvalues unless the forall body is aTypeAlias, otherwise emitUnsupportedOperation(“Xis not subscriptable”). - Add a regression test asserting
func[int](100)produces an error.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
pyrefly/lib/alt/expr.rs |
Changes subscript inference to reject subscripting generic functions/callables (non-type-alias Forall). |
pyrefly/lib/test/calls.rs |
Adds a regression testcase covering func[int](100) erroring as “not subscriptable”. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| Type::Forall(forall) => { | ||
| let tys = | ||
| xs.map(|x| self.expr_untype(x, TypeFormContext::TypeArgument, errors)); | ||
| self.specialize_forall(*forall, tys, range, errors) | ||
| if matches!(forall.body, Forallable::TypeAlias(_)) { | ||
| let tys = xs | ||
| .map(|x| self.expr_untype(x, TypeFormContext::TypeArgument, errors)); | ||
| self.specialize_forall(*forall, tys, range, errors) | ||
| } else { | ||
| let name = forall.body.name(); | ||
| self.error( |
There was a problem hiding this comment.
matches!(forall.body, ...) moves forall.body, so *forall / forall.body.name() in the branches will fail to compile due to a partial move. Use matches!(&forall.body, ...) (or pattern-match on &forall.body) and only move *forall when specializing.
|
@yangdanny97 has imported this pull request. If you are a Meta employee, you can view this in D95062809. |
stroxler
left a comment
There was a problem hiding this comment.
Review automatically exported from Phabricator review in Meta.
Summary
Fixes #2590
blocked subscripting generic functions/callables so func[int] now emits UnsupportedOperation, and added a regression test.
Test Plan