Skip to content

Fix false positive invalid-type-var for generic functions assigned to attributes (#2204)#2725

Closed
yangdanny97 wants to merge 1 commit intofacebook:mainfrom
yangdanny97:export-D95799931
Closed

Fix false positive invalid-type-var for generic functions assigned to attributes (#2204)#2725
yangdanny97 wants to merge 1 commit intofacebook:mainfrom
yangdanny97:export-D95799931

Conversation

@yangdanny97
Copy link
Contributor

Summary:
fixes #2204

Fixed check_and_sanitize_type_parameters to also exclude Forall-bound tparams within the type of the attribute.

Differential Revision: D95799931

… attributes (facebook#2204)

Summary:
fixes facebook#2204

Fixed check_and_sanitize_type_parameters to also exclude Forall-bound tparams within the type of the attribute.

Differential Revision: D95799931
@meta-cla meta-cla bot added the cla signed label Mar 9, 2026
@meta-codesync
Copy link

meta-codesync bot commented Mar 9, 2026

@yangdanny97 has exported this pull request. If you are a Meta employee, you can view the originating Diff in D95799931.

@github-actions
Copy link

github-actions bot commented Mar 9, 2026

Diff from mypy_primer, showing the effect of this PR on open source code:

jinja (https://github.com/pallets/jinja)
- ERROR src/jinja2/environment.py:352:14-21: Attribute `filters` cannot depend on type variable `K`, which is not in the scope of class `Environment` [invalid-type-var]
- ERROR src/jinja2/environment.py:352:14-21: Attribute `filters` cannot depend on type variable `_T`, which is not in the scope of class `Environment` [invalid-type-var]

@github-actions
Copy link

github-actions bot commented Mar 9, 2026

Primer Diff Classification

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

1 improvement(s) across jinja.

Project Verdict Changes Error Kinds Root Cause
jinja ✅ Improvement -1 invalid-type-var check_and_sanitize_type_parameters()
Detailed analysis

✅ Improvement (1)

jinja (-1)

This is an improvement. The error was a false positive - pyrefly was incorrectly flagging valid code where generic functions are assigned to class attributes. Looking at line 352, self.filters = DEFAULT_FILTERS.copy() assigns a dictionary of filter functions to the instance. Some of these functions are generic (have type parameters like K), but those type parameters are bound within the function's own scope (Forall quantification), not dependent on the Environment class's type parameters. The PR correctly identifies that type parameters bound by Forall nodes within attribute types should not trigger invalid-type-var errors, since they have their own independent scope. This matches how mypy and pyright handle generic functions assigned to attributes.
Attribution: The change to check_and_sanitize_type_parameters() in pyrefly/lib/alt/class/class_field.rs fixed this by excluding Forall-bound type parameters within the attribute type from the invalid-type-var check. The new collect_forall_tparams() function identifies generic functions (Forall types) and their type parameters, allowing them to be used in attribute assignments without triggering scope violations


Was this helpful? React with 👍 or 👎

Classification by primer-classifier (1 LLM)

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

@rchen152 rchen152 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.

@meta-codesync meta-codesync bot closed this in e3acc75 Mar 9, 2026
@meta-codesync
Copy link

meta-codesync bot commented Mar 9, 2026

This pull request has been merged in e3acc75.

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.

False positive invalid-type-var from check_and_sanitize_type_parameters

3 participants