Skip to content

A nominal type from a stub file should be treated as equivalent to a nominal type from a .py file with the same module name #1306

@AlexWaygood

Description

@AlexWaygood

Summary

We've been seeing strange diagnostics like this in ecosystem reports for bokeh, and they're starting to show up more as we add more functionality to ty:

src/bokeh/model/model.py:496:16: error[invalid-return-type] Return type does not match returned value: expected `set[src.bokeh.model.model.Model]`, found `set[src.bokeh.model.model.Model]`

A minimal repro of this issue looks something like the following:

`package/__init__.py`:

```py
from .foo import MyClass

def make_MyClass() -> MyClass:
    return MyClass()
```

`package/foo.pyi`:

```py
class MyClass: ...
```

`package/foo.py`:

```py
class MyClass: ...

def get_MyClass() -> MyClass:
    from . import make_MyClass

    # error: [invalid-return-type] "Return type does not match returned value: expected `package.foo.MyClass`, found `package.foo.MyClass`"
    return make_MyClass()
```

The issue is that ty:

  1. Views MyClass from the foo.py file and MyClass from foo.pyi as being different classes
  2. Understands the get_MyClass function as being annotated as returning an instance of MyClass from foo.py
  3. Understands the get_MyClass function as actually returning an instance of MyClass from foo.pyi

I propose that we fix this by adding handling to make two nominal-instance types equivalent to each other if the two types come from modules that have the same fully qualified module names and come from the same search path.

Version

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingstubsissues with understanding stub (pyi) filestyping semanticstyping-module features, spec compliance, etc

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions