Skip to content

TYP use typeddict to define cssdict #40947

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

Merged
merged 6 commits into from
Apr 27, 2021

Conversation

MarcoGorelli
Copy link
Member

xref #39942 (comment) - here's a solution, the docs say that

At runtime it is a plain dict

@MarcoGorelli MarcoGorelli added Typing type annotations, mypy/pyright type checking Styler conditional formatting using DataFrame.style labels Apr 14, 2021
Copy link
Contributor

@attack68 attack68 left a comment

Choose a reason for hiding this comment

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

lgtm - not sure about the failed tests though

@MarcoGorelli
Copy link
Member Author

Thanks @attack68 - I've merged master and the tests pass, so looks like they were unrelated

@jreback jreback added this to the 1.3 milestone Apr 15, 2021
@jreback
Copy link
Contributor

jreback commented Apr 15, 2021

@simonjayhawkins ok here?

for x in cast(str, style["selector"]).split(",")
[
CSSDict(selector=x, props=style["props"])
for x in style["selector"].split(",")
Copy link
Member

Choose a reason for hiding this comment

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

I don't think you should need to create the CSSDict explicitly. mypy should check if the expression is a compatible type

    return [
        {"selector": selector, "props": css_dict["props"]}
        for css_dict in styles
        for selector in css_dict["selector"].split(",")
    ]

Copy link
Member Author

Choose a reason for hiding this comment

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

just tried that and got

pandas/io/formats/style_render.py:624: error: List comprehension has incompatible type List[Dict[str, Union[str, List[Tuple[str, Union[str, int, float]]]]]]; expected List[CSSDict]  [misc]

Copy link
Member

Choose a reason for hiding this comment

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

it maybe a mypy cache thing.

maybe better then to use for loops and append starting with css_dict : CSSDict = []

the list comp with 4 fors is too difficult to grok.

Copy link
Member

Choose a reason for hiding this comment

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

hmm, seems ok

(pandas-dev) simon@P340:~/pandas (cssdict)$ mypy pandas
Success: no issues found in 1280 source files
(pandas-dev) simon@P340:~/pandas (cssdict)$ mypy pandas --no-incremental
Success: no issues found in 1280 source files
(pandas-dev) simon@P340:~/pandas (cssdict)$ mypy --version
mypy 0.812
(pandas-dev) simon@P340:~/pandas (cssdict)$ 

Copy link
Member

Choose a reason for hiding this comment

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

maybe better then to use for loops and append starting with css_dict : CSSDict = []

should have been css_styles : CSSStyles = []

Copy link
Member Author

@MarcoGorelli MarcoGorelli Apr 25, 2021

Choose a reason for hiding this comment

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

Seems like you're right!

Seems like

    ret = [
        {"selector": selector, "props": css_dict["props"]}
        for css_dict in styles
        for selector in css_dict["selector"].split(",")
    ]
    return ret

throws a mypy error, while

    return [
        {"selector": selector, "props": css_dict["props"]}
        for css_dict in styles
        for selector in css_dict["selector"].split(",")
    ]

doesn't - I wasn't expecting them to be different, and I had the former pattern, sorry about that

Copy link
Member

@simonjayhawkins simonjayhawkins Apr 26, 2021

Choose a reason for hiding this comment

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

yep. mypy will infer the type of the intermediate variable instead of checking the return expression against the return type. could use...

ret: CSSStyles = ...
return ret

and mypy will check the list comp is compatible the variable type declaration

Copy link
Member Author

Choose a reason for hiding this comment

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

thanks for explaining/merging! will copy-and-paste next time

Copy link
Member

@simonjayhawkins simonjayhawkins left a comment

Choose a reason for hiding this comment

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

Thanks @MarcoGorelli lgtm

@@ -70,6 +73,8 @@
else:
# typing.final does not exist until py38
final = lambda x: x
# typing.TypedDict does not exist until py38
TypedDict = dict
Copy link
Member

Choose a reason for hiding this comment

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

if we don't need to use the TypedDict at runtime, then we would not need this and we could only use TypedDict in TYPE_CHECKING blocks.

I see the downside being that aliases (and imports of aliases) would also need to be defined in the TYPE_CHECKING block and that we would not be able to construct the TypedDict explicitly in code. but that could be an upside.

no strong preference here yet.

Copy link
Member

Choose a reason for hiding this comment

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

thinking some more. leave this for now.

from NEP 29, Dec 26, 2021 drop support for Python 3.7

so we should be able to drop python 3.7 on master after the 1.3 release.

@simonjayhawkins simonjayhawkins merged commit 6a2f528 into pandas-dev:master Apr 27, 2021
@simonjayhawkins
Copy link
Member

Thanks @MarcoGorelli

@MarcoGorelli MarcoGorelli deleted the cssdict branch April 27, 2021 09:00
yeshsurya pushed a commit to yeshsurya/pandas that referenced this pull request May 6, 2021
JulianWgs pushed a commit to JulianWgs/pandas that referenced this pull request Jul 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Styler conditional formatting using DataFrame.style Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants