pydash icon indicating copy to clipboard operation
pydash copied to clipboard

Please add typing support

Open mvadari opened this issue 2 years ago • 6 comments

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.

mvadari avatar May 11 '22 15:05 mvadari

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 avatar May 12 '22 00:05 dgilland

@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

ghost avatar May 13 '22 18:05 ghost

@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 the typing 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 the typing standard lib is needed that was added after Python 3.6 (since 3.6 is currently the oldest Python supported by pydash).

dgilland avatar May 15 '22 21:05 dgilland

thanks @dgilland i'll follow accordingly

ghost avatar May 16 '22 18:05 ghost

I can defo help with testing.

povilasb avatar May 20 '22 14:05 povilasb

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

DeviousStoat avatar May 20 '22 15:05 DeviousStoat

Hello any updates?

mazlum avatar Nov 16 '22 08:11 mazlum

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 avatar Jan 30 '23 06:01 DeviousStoat

@DeviousStoat Good to close this as done?

dgilland avatar Mar 19 '23 00:03 dgilland

Yes sounds good to me, everything exposed should be typed now

DeviousStoat avatar Mar 19 '23 00:03 DeviousStoat

Done in

  • #177
  • #182
  • #183
  • #185
  • #186

Thanks again, @DeviousStoat!

dgilland avatar Mar 19 '23 01:03 dgilland

Hi, when will these updates be published in a new version?

mvadari avatar Apr 03 '23 14:04 mvadari

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.

dgilland avatar Apr 06 '23 02:04 dgilland

Type hints have been released as part of v7.0.0: https://pypi.org/project/pydash/7.0.0/

dgilland avatar Apr 12 '23 02:04 dgilland