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

[viz] Adding get_df typing #8034

Merged

Conversation

john-bodley
Copy link
Member

@john-bodley john-bodley commented Aug 12, 2019

CATEGORY

Choose one

  • Bug Fix
  • Enhancement (new features, refinement)
  • Refactor
  • Add tests
  • Build / Development Environment
  • Documentation

SUMMARY

I came across a bug in viz.py where get_csv which uses logic like,

def get_csv(self):
    df = self.get_df()
    include_index = not isinstance(df.index, pd.RangeIndex)
    ...

i.e., it assumes that df is of type pd.DataFrame whereas it can optionally be None. Going forward I'm not sure what the correct logic should be but I've added basic type hints to the get_df(...) method to inform developers that it can be either a pd.DataFrame or None. Note this PR does not fix the bug.

Note my preference would be for get_df(...) to only return a pd.DataFrame, that way we wouldn't need to write logic like if df is None in numerous places. One question is if df is None should the return value from get_csv() be None or should it be a serialized empty pd.DataFrame?

TEST PLAN

CI.

ADDITIONAL INFORMATION

  • Has associated issue:
  • Changes UI
  • Requires DB Migration.
  • Confirm DB Migration upgrade and downgrade tested.
  • Introduces new feature or API
  • Removes existing feature or API

REVIEWERS

to: @etr2460 @mistercrunch @villebro

Copy link
Member

@etr2460 etr2460 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 question. as a side note, i'm surprised that the typechecker still passes now that it knows you're trying to get a param on an optional value. Is that the desired behavior?

superset/viz.py Outdated
@@ -181,7 +182,7 @@ def get_samples(self):
df = self.get_df(query_obj)
return df.to_dict(orient="records")

def get_df(self, query_obj=None):
def get_df(self, query_obj: Dict[str, Any] = None) -> Optional[pd.DataFrame]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the query_obj be typed as an Optional[Dict[str, Any]]?

superset/viz.py Outdated
@@ -725,7 +726,7 @@ class MarkupViz(BaseViz):
def query_obj(self):
return None

def get_df(self, query_obj=None):
def get_df(self, query_obj: Dict[str, Any] = None) -> Optional[pd.DataFrame]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment here as above

@john-bodley
Copy link
Member Author

@etr2460 I addressed your comments.

@john-bodley john-bodley force-pushed the john-bodley--viz-get_df-typing branch from fc6e21e to 030adb1 Compare August 13, 2019 05:01
Copy link
Member

@etr2460 etr2460 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm with one more typing nit to fix

@@ -1854,7 +1859,7 @@ class IFrameViz(BaseViz):
def query_obj(self):
return None

def get_df(self, query_obj=None):
def get_df(self, query_obj: Dict[str, Any] = None) -> Optional[pd.DataFrame]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missed an optional here

@villebro villebro changed the title [viz] Adding get_def typing [viz] Adding get_df typing Aug 13, 2019
@villebro
Copy link
Member

I don't feel strongly either way, i.e. returning an empty DataFrame or None, but I think it's the correct approach to add typing now and make the change later if necessary. Which reminds me that there's a good bit of circular imports going on in db_engine_specsand models/core.py that showed up when I tried to add typing to db_engine_specs. Will try to get around to refactoring that at some point.

@etr2460
Copy link
Member

etr2460 commented Sep 9, 2019

@john-bodley are you planning on getting this in?

@john-bodley john-bodley merged commit 84bf5b4 into apache:master Sep 10, 2019
@john-bodley john-bodley deleted the john-bodley--viz-get_df-typing branch September 10, 2019 16:57
@mistercrunch mistercrunch added 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 0.35.0 labels Feb 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels size/S 🚢 0.35.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants