Comments (11)
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.
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.
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.
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.
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.
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.
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.
from pandas-stubs.
One last relevant item for the record, the stub used by pylance:
from pandas-stubs.
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.
from pandas-stubs.
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")
Note that the yellow squiggles indicate where pylance can't infer the types (I have that turned on in my environment).
from pandas-stubs.
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)
- Allow 'integer' in select_dtypes include argument HOT 1
- Unexpected, Misleading mypy Failures After pandas-stubs Update: DataFrame.from_dict() Behavior HOT 5
- Should from_dict be annotated to allow sequence data values? HOT 8
- Index.rename() should return Self HOT 3
- Annotation of `get_loc` does no longer fit to `iloc` resulting in many false positive type errors. HOT 2
- Can't pass `dict` to `DataFrame.apply` HOT 1
- `Series.map()` needs type for `arg` and result HOT 5
- pd.notna does not work with pyright strict mode. HOT 1
- `df.merge` partially unknown.
- `Series.astype` is partially unknown. HOT 2
- `df.drop` signature does not capture all constraints HOT 1
- Type of "apply" is partially unknown HOT 6
- Switch `to_timedelta` argument type from `list` to `Sequence` HOT 1
- Invalid typing for `df.to_csv` HOT 1
- read_csv: parameter usecols has wrong type hint HOT 17
- GroupBy[Series].count() should have return type Series[int] HOT 1
- Check incorrect when using a TimestampSeries and YearEnd from pandas.tseries.offsets HOT 1
- Pandas stubs forces numpy <2.0.0 for python < 3.12 HOT 1
- Restore nightly CI build due to new pandas version (3.0.0)
- Series constructor does not accept TypedDict HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pandas-stubs.