-
Notifications
You must be signed in to change notification settings - Fork 770
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
Code marked as unreachable after pandas concat #5630
Comments
Hello, I have the same problem, but I am on debian 12. How do i know it's pylance, when i disable pylance and reload, the code is suddenly reachable. My version of python is 3.12.1 in an environment conda. |
Hello same here with pylance v2024.3.1 each
|
Have a duplicate report here with additional details possibly: #5631 |
The matching overload of This overload was added in pandas-dev/pandas-stubs#858. Please file an issue against Btw, in this case there are multiple matching overloads because the input parameter is They seemt to be abusing |
Btw, while waiting for a fix, you could work around the @overload
def concat(
objs: Iterable[None] | Mapping[HashableT1, None],
*,
axis: Axis = ...,
join: Literal["inner", "outer"] = ...,
ignore_index: bool = ...,
keys: Iterable[HashableT2] = ...,
levels: Sequence[list[HashableT3] | tuple[HashableT3, ...]] = ...,
names: list[HashableT4] = ...,
verify_integrity: bool = ...,
sort: bool = ...,
copy: bool = ...,
) -> Never: ... |
Thanks for the explanation @debonte, I've submitted an issue to to Pandas devs here: pandas-dev/pandas-stubs#888 |
Note - both of the calls below will raise an exception with pandas, so in the example by the OP, pylance is absolutely correct in terms of what is reported! pd.concat()
pd.concat([]) |
FYI Pylance (through pandas-stubs) doesn't mark code as unreachable after the first code, only after the second one. |
Please see #5640 . pylance is doing the unreachable analysis by looking at the stubs even if type checking is off. |
I'm guessing that you had type checking turned off. Because if you turned it on, the first line would be flagged as a typing error. The second line will raise an exception, so then code below it is unreachable. What's debatable here is whether there should be some control over whether you can turn on/off the unreachable analysis, or whether pylance should be doing the unreachable analysis when you are not using type checking. |
Why is this (and #5631) closed? Is it fixed? If it is not, which open issue will fix the question? I will refer to #5631, but since that is duplicated to this one, the arguments should be relevant here. Pylance says it is ok to decide that it is unreachable code because pandas-stubs starts checking if all list is None then it will fail. So pylance is correct. I've been in both sides while reading the issues but now I feel like pandas-stubs are right and the issue is that if we have Any there is a possibility that we end with a unreachable code (all-None), but it isn't guaranteed. You can receive a list including some DataFrame. Don't you? from typing import Any
import pandas as pd
def generate_series() -> Any:
return pd.Series([1, 2, 3])
df = pd.concat({k: generate_series() for k in range(10)}, axis=1)
print("Unreachable?!") Thanks! |
I agree with the aspect of it not always being that easy. You could have a 3rd party library that is fully untyped (or improperly typed!) where the type checker cannot infer the result of a method or function from that library to be a |
Pyright and pylance are working as intended here (and in compliance with the Python type system). There's nothing actionable for the pylance or pyright teams, so it's appropriate to close this issue. The behavior you're seeing is due to the way the pandas stubs are using |
Is "reachability" part of the python type system? I don't see that in the link you provided. Remember, with the stubs as they are in You're saying they are incorrect in the context of doing "reachability analysis". The issue here is the assumptions about reachability inferred by |
@Dr-Irv, please see my recent reply on the pandas-stubs issue. While pyright doesn't emit any diagnostics about unreachability, the |
@debonte and see my response at pandas-dev/pandas-stubs#888 (comment) Command line pyright does not exhibit what you show (and neither does mypy). The fundamental issue is about the reachability analysis done within pylance/VSCode, which is why I opened #5640 |
As I've already said, correct type evaluation requires reachability analysis. One cannot be done without the other. You can't just "turn off reachability analysis" and expect to get correct types. |
But if a user turns off type analysis in VS Code, then shouldn't the reachability analysis also be turned off if one can't be done without the other? |
There is no way to turn off type analysis in Pylance. All functionality within Pylance (completion suggestions, hover text, signature help, semantic tokens, etc.) relies on type analysis. If you set |
OK, that makes sense. Maybe the option that is needed is "ignore stubs included with pylance" or "ignore specific packaged stubs included with pylance" ?? At least that would give people the option to temporarily suppress the issue if any stub package like |
That seems excessive to me. I suspect that a small percentage of our users have sufficient understanding of Python typing, stubs, Pylance/Pyright, etc. to know whether such a command would help in any particular scenario. Those that do would likely know enough to work around the issue in their code or in the stubs, thereby retaining the benefit of the unbroken/non-buggy portion of the stubs rather than throwing the baby out with the bath water. In the case of pandas-dev/pandas-stubs#888, they could cast the parameter passed to Btw, you pointed out that Pylance shipped with the stubs from your |
Whatever is in |
This worked like a charm: I think this is better than adding a flag saying "I don't want pylance using pandas-stub", so I wouldn't add that option. Thanks for your great support to both teams, pylance(-release) and pandas(-stubs). I love your projects! |
Hi guys is this fixed? Pandas concat messes up my editor viewing. |
It has been fixed in In the meantime you can use the workaround I mentioned above. |
A naive solution is to define my_concat function that returns pd.concat. Something like: def my_concat(dfs_list, axis=0): return pd.concat(dfs_list, axis=axis) df = my_concat([df_1, df_2], axis=0) |
This issue is also fixed in our most recent stable release -- 2024.3.2 |
Environment data
Code Snippet
Repro Steps
Expected behavior
Code flagged as unreachable, and thus greyed out
Actual behavior
Code should be reachable
Logs
I'm not sure what i should provide here, or if it's relevant. Let me know if more is required
The text was updated successfully, but these errors were encountered: