pydash
pydash copied to clipboard
Please add typing support
The repo that I use pydash in is typed, and pydash not supporting typing means that in a lot of places we need to ignore the types. This would be much easier to work with if pydash supported typing.
I'm not opposed to adding type hints, but I personally don't have enough free time to commit to it. Would be glad to help anyone that wants to add them, though.
@dgilland would be happy to help you on this, if you could guide me somehow.
PS: I'm new to this codebase.
I see you've annotated the type values in the docstring itself. Will ask you if I get stuck somewhere
@theredcap I've created a new branch typehints to get this started.
In the branch, I added the mypy
dev-dependency and configuration along with an invoke task to execute mypy that can be run with $ inv mypy
from the project root.
For some examples of how I've done type hints for some of my other projects:
- https://github.com/dgilland/shelmet
- https://github.com/dgilland/cacheout
- https://github.com/dgilland/sqlservice
The main "style" guidelines are:
- Use
import typing as t
to reference things from thetyping
module - Add a
types
submodule within the package if custom types are needed to be shared with other submodules - Use
typing_extensions
package for backwards compatibility if something from thetyping
standard lib is needed that was added after Python 3.6 (since 3.6 is currently the oldest Python supported by pydash).
thanks @dgilland i'll follow accordingly
I can defo help with testing.
Hey there! Some weeks ago I started working on this.
I experimented with several ideas for typing the chaining
interface as well (which I found a bit tricky) so it got the branch a bit messy, some part probably need some clean up and/or reviewing and it doesn't follow @dgilland style guidelines but I think many of the exposed functions are typed there. I believe this could help kickstart
https://github.com/DeviousStoat/pydash/tree/typed-pydash
Hello any updates?
Hello type enthusiasts!
Since there was not much updates I decided to work on it properly.
This branch is based on a rebased version of typehints. It has types for all the exposed functions following @dgilland 's guidelines (I hope).
Type tests
I added type tests using pytest-mypy-testing as I worked on it. This library is not very performant and latest released version doesn't work with latest mypy so I had to use master (the library doesn't seem actively maintained). Hence I am not sure if you will want to keep them but they helped me a lot while typing the library.
Also these type tests don't pass tox execution (all types are recognized as Any
in the tox environment) unless I remove this part from the test task:
tox_env_site_packages_dir = os.getenv("TOX_ENV_SITE_PACKAGES_DIR")
if tox_env_site_packages_dir:
# Re-path package source to match tox env so that we generate proper coverage report.
tox_env_pkg_src = os.path.join(tox_env_site_packages_dir, os.path.basename(PACKAGE_SOURCE))
args = args.replace(PACKAGE_SOURCE, tox_env_pkg_src)
I am not exactly sure why yet, I still need to look into that (maybe you have an idea @dgilland ?) Otherwise without this part, all is green from 3.6 to 3.11.
Limitations
There are some parts that I think are unfortunately not really typable like the path access to object. eg.
pydash.get({"hello": {"key": 5}}, "hello.key")
I don't think we can really understand the path (hello.key
). This would require the use of a specific type, something like
pydash.get({"hello": {"key": 5}}, PydashPath("hello").then("key"))
But this is for another discussion.
Help
This was overall quite tricky as I am far from a python typing expert and apart from some basic tests and the pytest tests I added I haven't really tested much yet. So there are probably quite a few problems and if some people excited by this could test it on their own I would really appreciate it.
Chaining
The chaining interface is not typed in the branch linked above as I think this deserved its own PR. I made it in a separate branch here based on the other one. This part is still very much a WIP but it seems to work for the most part if you want to test it too.
My solution is to make Chain
generic and have a script generating a stub file for a AllFuncs
interface that the Chain
class will implement. This class simply has the __getattr__
from current Chain
but the generated stub file will contain all the exposed typed functions of the library as methods with input and output types wrapped in Chain
.
If my understanding is correct this shouldn't affect execution whatsoever as the stub file will only be read by type checkers.
@DeviousStoat Good to close this as done?
Yes sounds good to me, everything exposed should be typed now
Done in
- #177
- #182
- #183
- #185
- #186
Thanks again, @DeviousStoat!
Hi, when will these updates be published in a new version?
New version should be out soon. Mostly just need to work through the changelog and then publish a new version. Hoping to have that by next week.
Type hints have been released as part of v7.0.0: https://pypi.org/project/pydash/7.0.0/