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

Type hints [WIP] #1297

Closed
wants to merge 2 commits into from
Closed

Conversation

Conengmo
Copy link
Member

I tried out adding type hints to the main modules (not the plugins).

Rationale
For users that use folium in an ad-hoc way the type hints may help in finding out what input a function accepts. And they may use an IDLE that gives those warnings automatically.

Users that write software with folium can now run Mypy on their software and get warnings for when their code is not compatible with folium.

For folium developers it becomes easier to work, because with explicit types you can change things with more confidence. And it forces you to think better about how a function should work.

Remarks

  • This makes folium in compatible with Python 3.5 because of some variable type hints that are necessary.
  • Many of the data inputs can be anything, a list, dict, Numpy array, Pandas dataframe, or something else... I typed these with Any, which is fine.

Changes

  • Split the validate_locations function into a version for normal lines and one for multi-lines. This makes it more robust for classes that use normal lines and the type hints more accurate.
  • path_options: define the two keyword arguments on usage. This is necessary for mypy and is more explicit as well. Change the radius argument from a bool/float into an optional float.
  • Mypy cannot handle function definitions that replace optional arguments, so I replaced these with lambdas.

Unrelated changes

  • get_obj_in_upper_tree had a bug. It assumed the top of a tree was reached if there's no _parent instance attribute. But actually all Element instances have this attribute, but it's None if there's no parent.

Merge plan
Don't merge this until we did a release. Ideally have Selenium tests up first so we can merge this with more confidence.

folium/features.py Show resolved Hide resolved
folium/features.py Show resolved Hide resolved
folium/features.py Show resolved Hide resolved
folium/features.py Outdated Show resolved Hide resolved
folium/folium.py Outdated Show resolved Hide resolved
folium/map.py Outdated Show resolved Hide resolved
folium/map.py Outdated Show resolved Hide resolved
folium/vector_layers.py Show resolved Hide resolved
@Conengmo
Copy link
Member Author

Conengmo commented Nov 7, 2022

Let's revisit this after we ran black on everything.

@Conengmo Conengmo closed this Nov 7, 2022
@Conengmo Conengmo mentioned this pull request Nov 30, 2022
@Conengmo Conengmo deleted the type-hints branch January 11, 2023 11:01
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