Skip to content

False positive invalid-type-var from check_and_sanitize_type_parameters #2204

@samwgoldman

Description

@samwgoldman

Describe the Bug

When we assign instance attributes, we need to check for unbound type variables, but the check is incorrectly also flagging type variables which are bound by the right-hand side expression.

from typing import reveal_type

def f[T](x: T) -> T:
    return x

class C:
    def m[U](self, x: U) -> U:
        return x

class D:
    def __init__(self, c: C):
        self.f = f # Unexpected invalid-type-var
        self.g = c.m # Unexpected invalid-type-var

def test(o: D):
    reveal_type(o.f) # should be [T](x: T) -> T
    reveal_type(o.g) # should be BoundMethod[C, [U](self: C, x: U) -> U]

I believe the error is happening here:

fn check_and_sanitize_type_parameters(

We collect the quantified types from the RHS expression, but we only want unbound quantifieds.

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN2UYANxiooAfSbEYAHXRzMMMHTABtACoBdABT5EddQEo6AWgB8BxHLo2BMBgFdK6OvjlyAxlFRw4dAMJWLraKyjSqAKo6cDBQYAA0rvoRxuZ0EUG2WXaOzq7u6F4%2BfgAimSFKdOLirBAM1doxcYke%2Bv6G5dl0TWCEygC8KtZd3bG9HIMehDQFoYzwDNq4%2BiUdwzaCImKSzDBLfcYAxN0AFrgOUJh02DB0Gjp6BqkW6ut2WxJSe7iEbEen50u11uACFzlgALL2M6YVT%2BRKRaJjNqJR4pUwWKIgeIgMiCMBQUiEBi0KAUY4ABVI%2BMJ3QwOAIdA8uHQkDYTlQDAgLMIcmOAGUYLcTgwGMQ4IgAPSSvFKQmEXhsSUwdCSzC4DxwSXM1kQdmUTnc1UqXh0VBCVDQVDYWBMllsjlcll0XDEJ3oOC89BkBhndAmESUOBGuiDGQgADMhAAjAAmcNyEAAXxxqA8XJEADFoDAKGgsHgiGRk0A

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions