-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add networkx stubs #10544
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
Add networkx stubs #10544
Conversation
This comment has been minimized.
This comment has been minimized.
I have attempted to fix all the issues! One problem I ran into is that mypy is very unhappy with the way I tried to express a common pattern in networkx which would be a lot easier with higher-kinded types: def builder_func(something: SomeCollection[T], create_using: Type[GenericGraph[T]]) -> GenericGraph[T]: ... GenericGraph here should be a TypeVar bounded to Another overload-related problem that mypy complains about is the pattern of changing the return type based on a flag parameter, True to enable returning a specialized subclass instead of the simpler base case. For example: @overload
def __call__(
self,
nbunch: NBunch[_Node] = ...,
data: Literal[False] = ...,
default: Incomplete = ...,
) -> OutEdgeView[_Node]: ...
@overload
def __call__(
self,
nbunch: NBunch[_Node] = ...,
data: Literal[True] = ...,
default: Incomplete = ...,
) -> OutEdgeDataView[_Node, tuple[_Node, _Node, dict[str, Incomplete]]]: ...
@overload
def __call__(
self, nbunch: NBunch[_Node] = ..., data: str = ..., default: _U | None = None
) -> OutEdgeDataView[_Node, tuple[_Node, _Node, _U]]: ... To me, this seems unambiguous. However, mypy is adamant that |
This comment has been minimized.
This comment has been minimized.
@rhelmot Are you still interested in completing this PR? My suggestions above should get you pretty close. I am interested in seeing networkx stubs into typeshed and out of microsoft/python-type-stubs. |
I am still interested, I just have a lot on my plate. If you'd like to push the project along, feel free to open a pull request to my fork. |
Co-authored-by: Avasam <[email protected]>
Co-authored-by: Avasam <[email protected]>
Co-authored-by: Avasam <[email protected]>
Co-authored-by: Avasam <[email protected]>
Co-authored-by: Avasam <[email protected]>
Co-authored-by: Avasam <[email protected]>
Co-authored-by: Avasam <[email protected]>
Co-authored-by: Avasam <[email protected]>
Co-authored-by: Avasam <[email protected]>
Co-authored-by: Avasam <[email protected]>
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I am trying to merge in the networkx 3.2 changes. this could take a moment. |
This comment has been minimized.
This comment has been minimized.
I would say this is ready to merge even though it still targets an old version. |
@rhelmot Quick update, since I am now a Contributor to the typeshed project on GitHub, I can update your branch directly and apply the above suggestions. If that's something you'd like to save you some time.
|
Yes, I would appreciate that, thank you! |
This comment has been minimized.
This comment has been minimized.
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot @rhelmot for the migration from https://github.com/fmagin/networkx-stubs !
This PR is now in a state I'd feel confortable merging knowing that it is not meant to be a complete stub. Any non-obvious typing decision have been documented as comments. There's a substantial amount of manual typing in some modules, so I'll give a couple weeks for other maintainers to take a quick look, see if I missed anything.
Once merged, the next steps as far as I'm concerned would be:
- Update to 3.2, see Add networkx stubs #10544 (comment) as to why that wasn't done in this PR.
- Use stubgen to add missing symbols and modules
- Ensure parity with https://github.com/microsoft/python-type-stubs/tree/main/stubs/networkx
It's been a couple weeks and I don't see any objection. In it goes! Thanks again @rhelmot . I'll probably take on parity with https://github.com/microsoft/python-type-stubs/tree/main/stubs/networkx next. |
networkx is a widely used library for graph analysis in scientific computing. It is a great candidate for type checking because it is absolutely full to the brim of container types. It is also unlikely to have types merged into upstream anytime soon. See discussion in the staging repository for this PR: fmagin/networkx-stubs#8
These stubs are by no means complete. They are complete for my personal use case (digraphs) and I spent about 3 hours trying to bang out correct stubs for everything else, but this library is gigantic. Everything I didn't get to remains the stubgen defaults. If this isn't the convention and I just just delete the unmodified stuff, please let me know!
The main concern I have is that this library has a few optional dependencies and has public interfaces which take types from these dependencies. These dependencies are declared as extras, so I believe the CI machinery here should be able to handle it, but some of them do not themselves have types available through any venue. I am not adding types to scipy. This triggers some type errors, and I don't know if those will be an issue. Please advise :)
cc @fmagin @Avasam