Skip to content

fix Python interpreter gets error, using [int] with a function call while pyrefly doesn't get error #2590#2598

Open
asukaminato0721 wants to merge 1 commit intofacebook:mainfrom
asukaminato0721:2590
Open

fix Python interpreter gets error, using [int] with a function call while pyrefly doesn't get error #2590#2598
asukaminato0721 wants to merge 1 commit intofacebook:mainfrom
asukaminato0721:2590

Conversation

@asukaminato0721
Copy link
Contributor

Summary

Fixes #2590

blocked subscripting generic functions/callables so func[int] now emits UnsupportedOperation, and added a regression test.

Test Plan

Summary: blocked subscripting generic functions/callables so func[int]
now emits UnsupportedOperation, and added a regression test.
@meta-cla meta-cla bot added the cla signed label Mar 1, 2026
@github-actions
Copy link

github-actions bot commented Mar 1, 2026

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]

@github-actions
Copy link

github-actions bot commented Mar 1, 2026

Primer Diff Classification

✅ 1 improvement(s) | 1 project(s) total

1 improvement(s) across mitmproxy.

Project Verdict Changes Error Kinds Root Cause
mitmproxy ✅ Improvement +2, -2 not-a-type, unsupported-operation expr_untype()
Detailed analysis

✅ Improvement (1)

mitmproxy (+2, -2)

This is an improvement. The new errors correctly catch a real bug - attempting to subscript generic functions like tutils.Placeholder[bytes] when tutils.Placeholder is being treated as a function rather than a class. Looking at the code, lines 89 and 91 show tutils.Placeholder[bytes] being used in type annotations, but the old errors suggested pyrefly was incorrectly inferring Placeholder as a function type rather than recognizing it as a generic class. The new unsupported-operation errors correctly identify that if something is inferred as a function, it cannot be subscripted. The removed errors were false positives - tutils.Placeholder[bytes] is valid syntax when Placeholder is a generic class, and the union with None is also valid. The PR change improved type inference to properly distinguish between generic classes (subscriptable) and generic functions (not subscriptable).
Attribution: The change to expr_untype() in pyrefly/lib/alt/expr.rs added a check that only allows subscripting for TypeAlias foralls, blocking subscripting of generic functions/callables. This correctly identifies that tutils.Placeholder (a generic class) should be subscriptable, while generic functions should not be.


Was this helpful? React with 👍 or 👎

Classification by primer-classifier (1 LLM)

@asukaminato0721 asukaminato0721 marked this pull request as ready for review March 1, 2026 21:24
Copilot AI review requested due to automatic review settings March 1, 2026 21:24
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

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::Forall values unless the forall body is a TypeAlias, otherwise emit UnsupportedOperation (“X is 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.

Comment on lines 1917 to +1924
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(
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
@meta-codesync
Copy link

meta-codesync bot commented Mar 3, 2026

@yangdanny97 has imported this pull request. If you are a Meta employee, you can view this in D95062809.

@yangdanny97 yangdanny97 self-assigned this Mar 3, 2026
Copy link
Contributor

@stroxler stroxler left a comment

Choose a reason for hiding this comment

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

Review automatically exported from Phabricator review in Meta.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python interpreter gets error, using [int] with a function call while pyrefly doesn't get error

4 participants