Giter VIP home page Giter VIP logo

Comments (11)

Dr-Irv avatar Dr-Irv commented on August 23, 2024
def foo2(df):
    return pd.DataFrame()

This declaration is incorrect. You call foo2() with no parameters, but you declare a parameter here.

So use

def foo2() -> pd.DataFrame:
    return pd.DataFrame()

and there is no problem.

from pandas-stubs.

lgonzalez-silen avatar lgonzalez-silen commented on August 23, 2024

The actual code is more complex, with the parameter actually being used. Let me see if I can come up with a more relevant example.

from pandas-stubs.

lgonzalez-silen avatar lgonzalez-silen commented on August 23, 2024

Ok. I simplified the example. I think it might be the destructuring that's the issue. The code now runs fine, but Pylance marks it unreachable. Understood that if I type the return list[pd.DataFrame | None] for the function, then no issues, but none of the code in my project is typed. Also, the actual function in my code can never return None. cc @Dr-Irv

from pandas-stubs.

Dr-Irv avatar Dr-Irv commented on August 23, 2024

Ok. I simplified the example. I think it might be the destructuring that's the issue. The code now runs fine, but Pylance marks it unreachable. Understood that if I type the return list[pd.DataFrame | None] for the function, then no issues, but none of the code in my project is typed. Also, the actual function in my code can never return None. cc @Dr-Irv

Not much we can do here. pylance is doing an analysis based on the stubs and inference of types that you haven't declared. It could be that their inference is incorrect. You're better off introducing types into your code so that pylance doesn't mark the code as unreachable.

I also put in your new example, and I'm not seeing the unreachable conclusion.

from pandas-stubs.

lgonzalez-silen avatar lgonzalez-silen commented on August 23, 2024

Ok. Thanks for looking anyway. Would you suggest I open an issue with Pylance? Here are my screenshots with the latest Pylance for the record.

Screenshot 2024-04-03 at 11 01 42 AM
Screenshot 2024-04-03 at 11 01 28 AM

So it is interpreting correctly as Iterable[None|Any]. Since you are not seeing the unreachable, does it mean panda-stubs handles the case, but Pylance doesn't?

from pandas-stubs.

lgonzalez-silen avatar lgonzalez-silen commented on August 23, 2024

Mirroring my code more closely (the accumulator var starts as None because it's a loop), I can definitely see an issue not related to anything pandas. Pylance thinks the code becomes unreachable within the loop, but then thinks things right outside the loop are fine... I'll open that issue over there.

Screenshot 2024-04-03 at 11 11 45 AM

from pandas-stubs.

lgonzalez-silen avatar lgonzalez-silen commented on August 23, 2024

One last relevant item for the record, the stub used by pylance:

Screenshot 2024-04-03 at 11 20 32 AM

from pandas-stubs.

Dr-Irv avatar Dr-Irv commented on August 23, 2024

Can you put this line in VSC: from pandas._version import _commit_hash then put your mouse over _commit_hash. If it doesn't appear, then you need to update pylance. I do think you're on an old version of the stubs.

from pandas-stubs.

lgonzalez-silen avatar lgonzalez-silen commented on August 23, 2024

Screenshot 2024-04-03 at 11 56 19 AM

from pandas-stubs.

Dr-Irv avatar Dr-Irv commented on August 23, 2024

So you're on the right version.

The type checking is inferring that df_new has an unknown type, and it seems that pylance is matching an unknown type to None. Here's an example that doesn't involve pandas that illustrates the issue you point out above (and makes it a pylance issue):

from typing import Literal
from typing_extensions import Never

def rlist():
    return [5, None]

def goo(x: Literal[None]) -> Never:
    raise Exception("not valid")

for _ in range(1):
    i, _ = rlist()
    goo(i)
    print("unreachable")
    
print("reachable")

image

Note that the yellow squiggles indicate where pylance can't infer the types (I have that turned on in my environment).

from pandas-stubs.

twoertwein avatar twoertwein commented on August 23, 2024

I would be open to removing the None->Never overload if it causes false negatives: we clearly can't catch similar bugs (empty lists).

from pandas-stubs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.