weaviate-python-client icon indicating copy to clipboard operation
weaviate-python-client copied to clipboard

Create stubs for mypy

Open hsm207 opened this issue 2 years ago • 19 comments
trafficstars

We should create a stub file so that mypy can lint code that uses this library

Refrences:

hsm207 avatar Apr 19 '23 06:04 hsm207

Hi @hsm207 I will love to help with this issue. I have already gone ahead and used stubgen to generate a draft. Are we only adding types for the weaviate-python-client/weaviate/ folder?

EteimZ avatar Jul 02 '23 07:07 EteimZ

Hi @EteimZ

cool, thanks for helping with this! Yes weaviate-python-client/weaviate is enough :)

dirkkul avatar Jul 02 '23 08:07 dirkkul

Hi @dirkkul

I forked the typeshed repo and added weaviate stubs to it. Here's the link to it. Can you got through it and tell me if there's any correction to be made before I make a pull request to the typeshed repo.

EteimZ avatar Jul 12 '23 09:07 EteimZ

Hi @EteimZ, sorry that your contribution was left to go stale! We've managed to get the client into a state of mypy compliance so would you be willing to regenerate your stub files with the latest main branch after which I'll give your repository a once over. Following that, you'll be able to open your PR into typesheds and we can get it merged!

Cheers 😁

tsmith023 avatar Sep 06 '23 15:09 tsmith023

Sure thing @tsmith023

EteimZ avatar Sep 07 '23 05:09 EteimZ

Hi @tsmith023, Here's the updated stub.

EteimZ avatar Sep 16 '23 00:09 EteimZ

@EteimZ, great news! Thanks for your help on this 😁

I will review once I get the opportunity as we're heavily into a sprint cycle currently!

tsmith023 avatar Sep 19 '23 11:09 tsmith023

Okay No Problem @tsmith023

EteimZ avatar Sep 19 '23 17:09 EteimZ

@EteimZ, I believe that the stubs all look fine provided that you called the standard mypy.stubgen command 😁

The only thing that jumps out is that the METADATA.toml seems outdated as the latest package version is v3.24.1. I will run mypy.stubtest on your repo in relation to our main and let you know if it throws any errors!

tsmith023 avatar Sep 22 '23 15:09 tsmith023

Alas! I found issues of inconsistency between the stubs and the package due to our misconfiguration of mypy within the pyproject.toml file. I have a PR setup to rectify this, after which we will release a patch with the new package!

tsmith023 avatar Sep 22 '23 16:09 tsmith023

@EteimZ, I believe that the stubs all look fine provided that you called the standard mypy.stubgen command 😁

The only thing that jumps out is that the METADATA.toml seems outdated as the latest package version is v3.24.1. I will run mypy.stubtest on your repo in relation to our main and let you know if it throws any errors!

The stub was generated from version v3.24. I forgot to update the METADATA.toml file.

EteimZ avatar Sep 25 '23 08:09 EteimZ

Alas! I found issues of inconsistency between the stubs and the package due to our misconfiguration of mypy within the pyproject.toml file. I have a PR setup to rectify this, after which we will release a patch with the new package!

Okay nice, just went through the PR.

EteimZ avatar Sep 25 '23 08:09 EteimZ

@EteimZ, looking at typeshed's CONTRIBUTING.md file here, I think we need to add the stubs used by this library into the requires field. These, as listed in requirements-devel.txt, are:

  • types-protobuf>=4.24.0.1<5.0.0
  • types-requests>=2.31.0.2<3.0.0
  • types-urllib3>=1.26.25.14<2.0.0

I'm not sure the exact format for how this should be so I'd recommend looking at other libraries in the typeshed repo to see how they format their METADATA.toml files 😁

Here's the requests library as an example

tsmith023 avatar Sep 25 '23 09:09 tsmith023

@EteimZ, there's one last thing that needs fixing. A better solution than adding # type: ignore to the auto-generated weaviate_grpc files is to create stubs for this folder. Could you stub it and add your generated stubs to your forked repo of typeshed? I believe once that and the above is done we can open the PR! 😁

tsmith023 avatar Sep 25 '23 09:09 tsmith023

@EteimZ, looking at typeshed's CONTRIBUTING.md file here, I think we need to add the stubs used by this library into the requires field. These, as listed in requirements-devel.txt, are:

* `types-protobuf>=4.24.0.1<5.0.0`

* `types-requests>=2.31.0.2<3.0.0`

* `types-urllib3>=1.26.25.14<2.0.0`

I'm not sure the exact format for how this should be so I'd recommend looking at other libraries in the typeshed repo to see how they format their METADATA.toml files 😁

Here's the requests library as an example

Okay @tsmith023 I will update the METADATA.toml.

EteimZ avatar Sep 26 '23 08:09 EteimZ

@EteimZ, there's one last thing that needs fixing. A better solution than adding # type: ignore to the auto-generated weaviate_grpc files is to create stubs for this folder. Could you stub it and add your generated stubs to your forked repo of typeshed? I believe once that and the above is done we can open the PR! 😁

The folder structure of the stub repo will be:

  • weaviate
    • weaviate
    • weaviate_grpc

Right?

EteimZ avatar Sep 26 '23 08:09 EteimZ

Hi @tsmith023, I went through the weaviate_grpc folder and I noticed it already contains .pyi stub files. Should I still go on and generate the stubs in my repo?

EteimZ avatar Oct 08 '23 17:10 EteimZ

Yes, I believe the stubs need to be propagated into the typeshed repo in order for them to be available to users when performing mypy static type checking

When you create the stubs for weaviate_grpc you should check whether they are identical to the ones already provided or if there has been a change

In any case, you can perform a sanity check as to whether the stubbing is correct by running mypy.stubtest in comparison between your generated stubs and the weaviate-python-client package itself

tsmith023 avatar Oct 09 '23 12:10 tsmith023

Hello, thanks for the great work! Any update on this issue?

Trinkes avatar Apr 29 '24 14:04 Trinkes

Closed by https://github.com/weaviate/weaviate-python-client/pull/1112, sorry for all the delay on this one! Static checking is available in v4.6.5 now 😁

tsmith023 avatar Jun 19 '24 11:06 tsmith023