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

Enable (partial) type hinting for downstream consumers #13380

Closed
2 tasks done
schuylermartin45 opened this issue Nov 30, 2023 · 4 comments
Closed
2 tasks done

Enable (partial) type hinting for downstream consumers #13380

schuylermartin45 opened this issue Nov 30, 2023 · 4 comments
Labels
pending::support indicates user is waiting on support from triage engineers source::anaconda created by members of Anaconda, Inc. type::feature request for a new feature or capability

Comments

@schuylermartin45
Copy link
Contributor

Checklist

  • I added a descriptive title
  • I searched open requests and couldn't find a duplicate

What is the idea?

We should enable or partially enable type hinting support for the sake of projects that use conda.

Why is this needed?

My team heavily relies on the VersionOrder class to perform comparisons between software version numbers. We have started using mypy to statically verify our python code against most (and eventually all) of our projects.

When I import the VersionOrder class and the InvalidVersionSpec exception, mypy treats them as Any types and can't resolve that they are classes.

I know some parts of the project use type hinting but it isn't universal. For my particular use case, I just need a few classes exposed to mypy.

What should happen?

From memory this should be as simple as adding a py.typed file to the conda/ directory or we could slowly roll it out and enable it in a few folders at a time.

Additional Context

I believe the PR is simple, but it might be a little controversial. Getting full and accurate type hints for the entire conda project is a obviously a big ask and not what I need right now. BUT, saying we support full type hints when we don't could also be confusing and problematic for others.

For now, I can suppress mypy in my project as a work around. But it would be nice not to have to disable static analyzer checks everytime I try to use this class.

@schuylermartin45 schuylermartin45 added the type::feature request for a new feature or capability label Nov 30, 2023
@travishathaway travishathaway added the source::anaconda created by members of Anaconda, Inc. label Dec 1, 2023
@travishathaway
Copy link
Contributor

Hi @schuylermartin45,

As I started working on my conda-auth plugin, I also ran into to mypy errors, see here:

I have since added py.typed file to the conda project, so I believe everything is already in place. This has been available since version 23.9.0. Are you perhaps using an older version of conda?

If there's something else that needs to be done to make these classes more "typing friendly", we'd be happy to help make that happen. Feel free to submit your own pull requests too.

@travishathaway travishathaway added the pending::feedback indicates we are waiting on feedback from the user label Dec 1, 2023
@schuylermartin45
Copy link
Contributor Author

I completely missed that file when I briefly searched for it yesterday. This is what I get for not using search.

I appear to be on 23.10.0. Here are the messages I get from mypy:

anaconda_packaging_utils/api/repodata_api.py:206: error: Call to untyped function "VersionOrder" in typed context 
[no-untyped-call]
                self_v = VersionOrder(self.version)
                         ^~~~~~~~~~~~~~~~~~~~~~~~~~
anaconda_packaging_utils/api/repodata_api.py:207: error: Call to untyped function "VersionOrder" in typed context 
[no-untyped-call]
                other_v = VersionOrder(other.version)
                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~
anaconda_packaging_utils/api/repodata_api.py:209: error: Expression has type "Any"  [misc]
                if self_v == other_v:
                   ^~~~~~~~~~~~~~~~~
anaconda_packaging_utils/api/repodata_api.py:211: error: Returning Any from function declared to return "bool" 
[no-any-return]
                return self_v < other_v
                ^~~~~~~~~~~~~~~~~~~~~~~
anaconda_packaging_utils/api/repodata_api.py:211: error: Expression has type "Any"  [misc]
                return self_v < other_v
                       ^~~~~~~~~~~~~~~~
anaconda_packaging_utils/api/repodata_api.py:212: error: Expression type contains "Any" (has type
"type[InvalidVersionSpec]")  [misc]
            except InvalidVersionSpec:

I guess what its actually mad about is that the parameters to the constructor are untyped and I just made an assumption typing wasn't being presented in the package. Still not sure what the issue with the exception is, but I can make a quick PR to add type hints to VersionOrder at least.

@conda-bot conda-bot added pending::support indicates user is waiting on support from triage engineers and removed pending::feedback indicates we are waiting on feedback from the user labels Dec 1, 2023
@schuylermartin45
Copy link
Contributor Author

Ah, it seems none of the exceptions are typed, so the Any is coming from the exception constructor (and maybe inheritance?) as well.

schuylermartin45 added a commit to schuylermartin45/conda that referenced this issue Dec 1, 2023
- Adds type hinting discussed in Issue conda#13380
- Also adds type hints for `InvalidVersionSpec` and related exceptions
jezdez pushed a commit that referenced this issue Dec 5, 2023
* Adds type hinting for `VersionOrder` class

- Adds type hinting discussed in Issue #13380
- Also adds type hints for `InvalidVersionSpec` and related exceptions

* Triggering CLA check
@schuylermartin45
Copy link
Contributor Author

Closing as the type hinting PR got merged and should be sufficient for my needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending::support indicates user is waiting on support from triage engineers source::anaconda created by members of Anaconda, Inc. type::feature request for a new feature or capability
Projects
Archived in project
Development

No branches or pull requests

3 participants