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

Fix type hints after adding Branca type checking #2060

Merged
merged 13 commits into from
Dec 29, 2024

Conversation

Conengmo
Copy link
Member

@Conengmo Conengmo commented Dec 29, 2024

In the recent Branca release we enabled type checking. This leads to issues when type checking Folium, since it relies on Branca. This PR addresses these issues.

Each commit in this PR contains a single fix, so you can walk through the commits to see what changes.

Render function

Note about the render function: I removed the return type, which effectively disables type checking. This is unfortunately necessary. In Branca we specify this function should return a string, but that's not what's done in MacroElement and so basically everything in Folium. We should update the type in Branca, do a release, then add the types back in Folium. This is a lot of trouble so maybe not worth the effort.

Bounds

The return type of bounds functions should allow for None values to be consistent. Even if we're sure a function returns floats not None, we still need to type it as optional. That's because the values are returned in a list, which is mutable.

Another thing is that some functions accept a bounds argument, which can be a list or tuple, but must have non-None values. Those types are distinct from bounds as return values, which should be list of list of optional float. Split the two in distinct types.

VegaLite

In practice, a VegaLite only works if it's added to a Figure, Div or Popup. We only have examples of that in our documentation, but it's not explicitly listed. Update the code to raise a meaningful error when this element is added to the wrong parent. This also then satisfies Mypy.

@Conengmo
Copy link
Member Author

@hansthen could you maybe take a look? This fixes the Mypy issues. If we get this merged, I'll make another patch release, since typing in Folium is broken in the latest release.

@Conengmo Conengmo requested a review from hansthen December 29, 2024 12:37
Copy link
Collaborator

@hansthen hansthen left a comment

Choose a reason for hiding this comment

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

I have some suggested changes but I am also fine to do this as is for now.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd prefer to rename these methods, since they really do something different than branca.Element.render. See the discussion I started on the signature redefinition.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I do not understand all the typing issues fully, but in general it looks good. I don't mind going ahead with this as is to fix the immediate issues. We can discuss my proposal to rename render from MacroElement and its children at a later moment. Whatever you think is best for now.

@Conengmo
Copy link
Member Author

Thanks for the review!

You're right about render: the type issues surface the larger issue. I'd prefer to get this fix out so the type checks don't fail for our user. Then we can look at how to address this more structurally next.

@Conengmo Conengmo merged commit ccb7509 into python-visualization:main Dec 29, 2024
11 checks passed
@Conengmo Conengmo deleted the fix-type-hints-branca branch December 29, 2024 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants