Skip to content

Commit

Permalink
Make mypy green, improve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mwaskom committed May 20, 2021
1 parent 718a191 commit 60222d5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
1 change: 1 addition & 0 deletions seaborn/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1428,6 +1428,7 @@ class VariableType(UserString):
them. If that changes, they should be more verbose.
"""
# TODO we can replace this with typing.Literal on Python 3.8+
allowed = "numeric", "datetime", "categorical"

def __init__(self, data):
Expand Down
30 changes: 14 additions & 16 deletions seaborn/_new_core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import annotations
from typing import Union, Optional, List, Dict, Tuple
from typing import Any, Union, Optional, List, Dict, Tuple
from collections.abc import Hashable, Sequence, Mapping, Sized
from numbers import Number

Expand Down Expand Up @@ -232,17 +232,14 @@ def _assign_variables_longform(
if data is None:
data = {}

# TODO should we try a data.to_dict() or similar here to more
# generally accept objects with that interface?
# Note that dict(df) also works for pandas, and gives us what we
# want, whereas DataFrame.to_dict() gives a nested dict instead of
# a dict of series.

# Variables can also be extracted from the index attribute
# TODO is this the most general way to enable it?
# There is no index.to_dict on multiindex, unfortunately
if hasattr(data, "index"):
index = data.index.to_frame() # type: ignore # mypy/1424
# TODO Generally interested in accepting a generic DataFrame interface
# Track https://data-apis.org/ for development

# Variables can also be extracted from the index of a DataFrame
index: Dict[str, Any]
if isinstance(data, pd.DataFrame):
index = data.index.to_frame().to_dict(
"series") # type: ignore # data-sci-types wrong about to_dict return
else:
index = {}

Expand All @@ -254,8 +251,8 @@ def _assign_variables_longform(

# Try to treat the argument as a key for the data collection.
# But be flexible about what can be used as a key.
# Usually it will be a string, but allow numbers or tuples too when
# taking from the main data object. Only allow strings to reference
# Usually it will be a string, but allow other hashables when
# taking from the main data object. Allow only strings to reference
# fields in the index, because otherwise there is too much ambiguity.
try:
val_as_data_key = (
Expand All @@ -267,8 +264,6 @@ def _assign_variables_longform(

if val_as_data_key:

# We know that __getitem__ will work

if val in data:
plot_data[key] = data[val] # type: ignore # fails on key: Hashable
elif val in index:
Expand All @@ -278,6 +273,9 @@ def _assign_variables_longform(
elif isinstance(val, str):

# This looks like a column name but we don't know what it means!
# TODO improve this feedback to distinguish between
# - "you passed a string, but did not pass data"
# - "you passed a string, it was not found in data"

err = f"Could not interpret value `{val}` for parameter `{key}`"
raise ValueError(err)
Expand Down

0 comments on commit 60222d5

Please sign in to comment.