Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can type inference in fall back to .py file if it cannot infer the type of a certain variable from .pyi file? #6808

Closed
wzm9856 opened this issue Jan 2, 2025 · 1 comment

Comments

@wzm9856
Copy link

wzm9856 commented Jan 2, 2025

I created this minimal demo to illustrate my problem.

Image

The variable "entity" is only decalred its type in AA.py, but not in AA.pyi, so when I hover over AA.A.entity in other files, Pylance only assigned "Any" to it.

What I want to do is when Pylance can't infer the type of a certain variable, it can automatically fall back to .py file to infer the type. In this case, "float" as declared in AA.py.

Or is there another way for me to do this? I already tried to import AA in AA.pyi and make class A inherit from AA.A, assuming this will take me from AA.pyi to AA.py, but nothing seems to happen.

The reason why I have this problem is that, I'm currently working on a giant project with absolute no typing in it. I want to generate stub files only for some variables that is tricky for pylance to infer. And for those conventional ones, I want pylance to infer from the py file, so I don't have to generate all variables in the stub file.

@rchiodo
Copy link
Contributor

rchiodo commented Jan 6, 2025

I believe what you're asking for isn't possible. Pylance defaults to using the pyi file if it exists. It's on a per module basis. If module A.pyi exists, it will be used in place of A.py to find types.

So, you have to pick one or the other per module.

Here's an example:

export_type.py:

def foo() -> int
    return 42

export_type.pyi

def foo() -> str
    ...

Image

@rchiodo rchiodo closed this as completed Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants