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

Code is unreachable message for reachable and running code #2476

Closed
ChamanAgrawal opened this issue Mar 15, 2022 · 9 comments
Closed

Code is unreachable message for reachable and running code #2476

ChamanAgrawal opened this issue Mar 15, 2022 · 9 comments
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@ChamanAgrawal
Copy link

Environment data

  • Language Server version: 2022.3.1
  • OS and version: Windows 10 Home Single Language, 21H1
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.6.8

Expected behaviour

Using influxdb==5.3.1 (https://github.com/influxdata/influxdb-python)
When I declare an instance of class influxdb.DataFrameClient , Pylance declares the code below declaration as unreachable, but on running the file; the code actually works. Similar thing also happens when using jupyter Notebook in vscode.

image

image

Actual behaviour

Since the code runs, it should be visible as reachable section.

@erictraut
Copy link
Contributor

Please provide a minimal self-contained sample in code form that exhibits the problem. It's not really possible to diagnose problems like this from screen shots of partial code.

@ChamanAgrawal
Copy link
Author

Hi @erictraut,
Here is a small piece of running code. I am also attaching the screenshot again because that is the only way I can think of to show the problem I have Code in unreachable [Pylance].

from influxdb import DataFrameClient

influx_client = DataFrameClient()
print('Last line running')

image

You can check the code by just running it, it prints the last print statement.
Do let me know I can do anything else, to clarify things. Thanks!

@erictraut
Copy link
Contributor

Thanks, that's very helpful. I'm able to repro the problem, and I'll take a look.

@erictraut
Copy link
Contributor

The issue here is that the influxdb code contains some unusual exception handling code to handle the case where pandas is not installed. Inside of an exception handler, it declares a proxy class called DataFrameClient that always raises an exception when its constructor is called. In the non-exception path (where pandas is successfully imported), DataFrameClient is imported from another sub-module. Pyright (the type checker that underlies pylance) uses some heuristics to determine which type definition to use in ambiguous (dynamic) cases like this. The current heuristic is preferring the proxy class in the exception path. I've updated the heuristic to prefer the non-exception path. This addresses the issue you're seeing. This change will be included in the next release.

@erictraut erictraut added bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version and removed triage labels Mar 15, 2022
@ChamanAgrawal
Copy link
Author

The issue here is that the influxdb code contains some unusual exception handling code to handle the case where pandas is not installed. Inside of an exception handler, it declares a proxy class called DataFrameClient that always raises an exception when its constructor is called. In the non-exception path (where pandas is successfully imported), DataFrameClient is imported from another sub-module. Pyright (the type checker that underlies pylance) uses some heuristics to determine which type definition to use in ambiguous (dynamic) cases like this. The current heuristic is preferring the proxy class in the exception path. I've updated the heuristic to prefer the non-exception path. This addresses the issue you're seeing. This change will be included in the next release.

Great, thanks a lot

@bschnurr
Copy link
Member

This issue has been fixed in version 2022.3.2, which we've just released. You can find the changelog here: CHANGELOG.md

@enryH
Copy link

enryH commented Feb 28, 2023

I have the same issue using the CollabDataLoaders from fastai. Example:

from dataclasses import dataclass, field
from fastai.collab import *
import pandas as pd


@dataclass
class DataSplits():
    is_wide_format: bool = field(init=True, repr=False)
    train_X: pd.DataFrame = None
    val_y: pd.DataFrame = None
    test_y: pd.DataFrame = None
    

def combine_data(train_df: pd.DataFrame, val_df: pd.DataFrame) -> Tuple[pd.DataFrame, float]:
    """Helper function to combine training and validation data in long-format. The 
    training and validation data will be mixed up in CF training as the sample
    embeddings have to be trained for all samples. The returned frac can be used to have
    the same number of (non-missing) validation samples as before.

    Parameters
    ----------
    train_df : pd.DataFrame
        Consecutive training data in long-format, each row having (unit, feature, value)
    val_df : pd.DataFrame
        Consecutive training data in long-format, each row having (unit, feature, value)

    Returns
    -------
    Tuple[pd.DataFrame, List[list, list]]
        Pandas DataFrame of concatenated samples of training and validation data.
        Fraction of samples originally in validation data.
    """
    X = train_df.append(val_df).reset_index()
    frac = len(val_df) / (len(train_df)+len(val_df))
    return X, frac

class CollabAnalysis():

    def __init__(self,
                 datasplits: DataSplits,
                 sample_column='Sample ID',
                 item_column='peptide',
                 target_column='intensity',
                 model_kwargs=dict(),
                 batch_size=64):
        self.X, self.frac = combine_data(datasplits.train_X,
                                         datasplits.val_y)
        self.batch_size = batch_size
        # if I comment out the next block ("self.dls") the lines after are reached.
        self.dls = CollabDataLoaders.from_df(self.X, valid_pct=self.frac,
                                             seed=42,
                                             user_name=sample_column,
                                             item_name=item_column,
                                             rating_name=target_column,
                                             bs=self.batch_size)
        # not reachable
        user_name=sample_column
        item_name=item_column
        rating_name=target_column
        # here it continues, but maybe not so interesting...

At runtime everything works as expected.

@debonte
Copy link
Contributor

debonte commented Feb 28, 2023

@enryH, I'm unable to repro this. Can you please file a new issue?

@enryH
Copy link

enryH commented Mar 1, 2023

@debonte I cannot reproduce it either when I load the environment in a codespace. So might be due to Windows and an old python/fastai version... I'll see if I re-create the environment if the issue pertains and in that case open a new issue.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

5 participants