Merge networkx from python-type-stubs
This PR merges the stubs from https://github.com/microsoft/python-type-stubs/tree/main/stubs/networkx so that removing them from there produce no regression for Pylance users.
I didn't make much effort in completing annotations other than validating what I was copying over. In doing that I did find quite a handful of incorrect annotations though.
I've already started a few PRs, and will create more that have specific scopes to make it easier to review these changes in chunks. (and will serve as self-review for this big PR). Until then, I'll keep this as draft.
Diff from mypy_primer, showing the effect of this PR on open source code:
bokeh (https://github.com/bokeh/bokeh)
- src/bokeh/sphinxext/bokeh_palette_group.py:53: note: In module imported here:
- src/bokeh/util/serialization.py: note: In function "convert_datetime_type":
- src/bokeh/util/serialization.py:167:1: error: Argument 1 to "convert_datetime_type" becomes "Any | Any | Any | datetime | date | time | datetime64[date | int | None]" due to an unfollowed import [no-any-unimported]
- src/bokeh/util/serialization.py: note: In function "transform_series":
- src/bokeh/util/serialization.py:362:1: error: Argument 1 to "transform_series" becomes "Any | Any | Any" due to an unfollowed import [no-any-unimported]
- src/bokeh/core/property/data_frame.py: note: In class "EagerDataFrame":
- src/bokeh/core/property/data_frame.py:50:22: error: Base type becomes "Property[Any]" due to an unfollowed import [no-any-unimported]
- src/bokeh/core/property/data_frame.py: note: At top level:
- src/bokeh/core/property/data_frame.py:63: error: Unused "type: ignore" comment [unused-ignore]
- src/bokeh/core/property/data_frame.py: note: In class "EagerSeries":
- src/bokeh/core/property/data_frame.py:68:19: error: Base type becomes "Property[Any]" due to an unfollowed import [no-any-unimported]
- src/bokeh/core/property/data_frame.py: note: At top level:
- src/bokeh/core/property/data_frame.py:81: error: Unused "type: ignore" comment [unused-ignore]
- src/bokeh/core/property/data_frame.py: note: In class "PandasDataFrame":
- src/bokeh/core/property/data_frame.py:86:23: error: Base type becomes "Property[Any]" due to an unfollowed import [no-any-unimported]
- src/bokeh/core/property/data_frame.py: note: In class "PandasGroupBy":
- src/bokeh/core/property/data_frame.py:109:21: error: Base type becomes "Property[Any]" due to an unfollowed import [no-any-unimported]
- src/bokeh/io/export.py: note: In function "get_screenshot_as_png":
- src/bokeh/io/export.py:223:1: error: Return type becomes "Any" due to an unfollowed import [no-any-unimported]
- src/bokeh/util/hex.py: note: In function "hexbin":
- src/bokeh/util/hex.py:158:1: error: Return type becomes "Any" due to an unfollowed import [no-any-unimported]
+ src/bokeh/models/util/structure.py:165: note: ... from here:
hydpy (https://github.com/hydpy-dev/hydpy)
- hydpy/core/variabletools.py:1480: error: Need type annotation for "array" [var-annotated]
- hydpy/core/testtools.py:1516: error: Incompatible return value type (got "dict[ModelSequence, ndarray[tuple[int, int], dtype[float64]]]", expected "dict[ModelSequence, ndarray[tuple[int, ...], dtype[float64]]]") [return-value]
- hydpy/core/testtools.py:1516: note: "Dict" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
- hydpy/core/testtools.py:1516: note: Consider using "Mapping" instead, which is covariant in the value type
- hydpy/core/testtools.py:1516: note: Perhaps you need a type annotation for "yvalues"? Suggestion: "dict[ModelSequence, ndarray[tuple[int, ...], dtype[float64]]]"
- hydpy/core/masktools.py:106: error: Argument 1 of "__contains__" is incompatible with supertype "ndarray"; supertype defines the argument type as "object" [override]
- hydpy/core/masktools.py:106: note: This violates the Liskov substitution principle
- hydpy/core/masktools.py:106: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
- hydpy/core/itemtools.py:951: error: Argument 2 to "update_variable" of "ChangeItem" has incompatible type "float64"; expected "ndarray[tuple[int, ...], dtype[float64]]" [arg-type]
- hydpy/core/itemtools.py:954: error: Argument 2 to "update_variable" of "ChangeItem" has incompatible type "float64"; expected "ndarray[tuple[int, ...], dtype[float64]]" [arg-type]
- hydpy/core/hydpytools.py:3081: error: Need type annotation for "digraph" [var-annotated]
- hydpy/auxs/statstools.py:298: error: Argument "sim" to "SimObs" has incompatible type "Series[Any]"; expected "ndarray[tuple[int, ...], dtype[float64]]" [arg-type]
- hydpy/auxs/statstools.py:298: error: Argument "obs" to "SimObs" has incompatible type "Series[Any]"; expected "ndarray[tuple[int, ...], dtype[float64]]" [arg-type]
- hydpy/auxs/statstools.py:1486: error: No overload variant of "fmin" matches argument types "Callable[[tuple[float, float, float, float]], float]", "Iterable[float]", "float", "float", "bool" [call-overload]
- hydpy/auxs/statstools.py:1486: note: Possible overload variants:
- hydpy/auxs/statstools.py:1486: note: def fmin(func: Callable[[ndarray[tuple[int], dtype[float64]], VarArg(Any), KwArg(Any)], float | floating[Any] | integer[Any] | numpy.bool[builtins.bool]], x0: float | floating[Any] | integer[Any] | numpy.bool[builtins.bool] | CanArrayND[floating[Any] | integer[Any] | numpy.bool[builtins.bool], tuple[int, ...]] | Sequence[float | floating[Any] | integer[Any] | numpy.bool[builtins.bool] | CanArray0D[floating[Any] | integer[Any] | numpy.bool[builtins.bool]]], args: tuple[object, ...] = ..., xtol: float | floating[Any] | integer[Any] | numpy.bool[builtins.bool] = ..., ftol: float | floating[Any] | integer[Any] | numpy.bool[builtins.bool] = ..., maxiter: int | None = ..., maxfun: int | None = ..., full_output: Literal[False, 0] = ..., disp: Literal[0, 1, 2, 3] | builtins.bool | numpy.bool[builtins.bool] = ..., retall: Literal[False, 0] = ..., callback: Callable[[ndarray[tuple[int], dtype[float64]]], None] | None = ..., initial_simplex: CanArrayND[floating[Any] | integer[Any] | numpy.bool[builtins.bool], tuple[int, ...]] | Sequence[CanArrayND[floating[Any] | integer[Any] | numpy.bool[builtins.bool], tuple[int, ...]] | Sequence[float | floating[Any] | integer[Any] | numpy.bool[builtins.bool] | CanArray0D[floating[Any] | integer[Any] | numpy.bool[builtins.bool]]]] | None = ...) -> ndarray[tuple[int], dtype[float64]]
- hydpy/auxs/statstools.py:1486: note: def fmin(func: Callable[[ndarray[tuple[int], dtype[float64]], VarArg(Any), KwArg(Any)], float | floating[Any] | integer[Any] | numpy.bool[builtins.bool]], x0: float | floating[Any] | integer[Any] | numpy.bool[builtins.bool] | CanArrayND[floating[Any] | integer[Any] | numpy.bool[builtins.bool], tuple[int, ...]] | Sequence[float | floating[Any] | integer[Any] | numpy.bool[builtins.bool] | CanArray0D[floating[Any] | integer[Any] | numpy.bool[builtins.bool]]], args: tuple[object, ...] = ..., xtol: float | floating[Any] | integer[Any] | numpy.bool[builtins.bool] = ..., ftol: float | floating[Any] | integer[Any] | numpy.bool[builtins.bool] = ..., maxiter: int | None = ..., maxfun: int | None = ..., full_output: Literal[False, 0] = ..., disp: Literal[0, 1, 2, 3] | builtins.bool | numpy.bool[builtins.bool] = ..., *, retall: Literal[True, 1], callback: Callable[[ndarray[tuple[int], dtype[float64]]], None] | None = ..., initial_simplex: CanArrayND[floating[Any] | integer[Any] | numpy.bool[builtins.bool], tuple[int, ...]] | Sequence[CanArrayND[floating[Any] | integer[Any] | numpy.bool[builtins.bool], tuple[int, ...]] | Sequence[float | floating[Any] | integer[Any] | numpy.bool[builtins.bool] | CanArray0D[floating[Any] | integer[Any] | numpy.bool[builtins.bool]]]] | None = ...) -> tuple[ndarray[tuple[int], dtype[float64]], list[ndarray[tuple[int], dtype[signedinteger[_32Bit | _64Bit]]] | ndarray[tuple[int], dtype[float64]]]]
- hydpy/auxs/statstools.py:1486: note: def fmin(func: Callable[[ndarray[tuple[int], dtype[float64]], VarArg(Any), KwArg(Any)], float | floating[Any] | integer[Any] | numpy.bool[builtins.bool]], x0: float | floating[Any] | integer[Any] | numpy.bool[builtins.bool] | CanArrayND[floating[Any] | integer[Any] | numpy.bool[builtins.bool], tuple[int, ...]] | Sequence[float | floating[Any] | integer[Any] | numpy.bool[builtins.bool] | CanArray0D[floating[Any] | integer[Any] | numpy.bool[builtins.bool]]], args: tuple[object, ...] = ..., xtol: float | floating[Any] | integer[Any] | numpy.bool[builtins.bool] = ..., ftol: float | floating[Any] | integer[Any] | numpy.bool[builtins.bool] = ..., maxiter: int | None = ..., maxfun: int | None = ..., *, full_output: Literal[True, 1], disp: Literal[0, 1, 2, 3] | builtins.bool | numpy.bool[builtins.bool] = ..., retall: Literal[False, 0] = ..., callback: Callable[[ndarray[tuple[int], dtype[float64]]], None] | None = ..., initial_simplex: CanArrayND[floating[Any] | integer[Any] | numpy.bool[builtins.bool], tuple[int, ...]] | Sequence[CanArrayND[floating[Any] | integer[Any] | numpy.bool[builtins.bool], tuple[int, ...]] | Sequence[float | floating[Any] | integer[Any] | numpy.bool[builtins.bool] | CanArray0D[floating[Any] | integer[Any] | numpy.bool[builtins.bool]]]] | None = ...) -> tuple[ndarray[tuple[int], dtype[float64]], float | floating[Any] | integer[Any] | numpy.bool[builtins.bool], int, int, Literal[0, 1, 2, 3, 4]]
- hydpy/auxs/statstools.py:1486: note: def fmin(func: Callable[[ndarray[tuple[int], dtype[float64]], VarArg(Any), KwArg(Any)], float | floating[Any] | integer[Any] | numpy.bool[builtins.bool]], x0: float | floating[Any] | integer[Any] | numpy.bool[builtins.bool] | CanArrayND[floating[Any] | integer[Any] | numpy.bool[builtins.bool], tuple[int, ...]] | Sequence[float | floating[Any] | integer[Any] | numpy.bool[builtins.bool] | CanArray0D[floating[Any] | integer[Any] | numpy.bool[builtins.bool]]], args: tuple[object, ...] = ..., xtol: float | floating[Any] | integer[Any] | numpy.bool[builtins.bool] = ..., ftol: float | floating[Any] | integer[Any] | numpy.bool[builtins.bool] = ..., maxiter: int | None = ..., maxfun: int | None = ..., *, full_output: Literal[True, 1], disp: Literal[0, 1, 2, 3] | builtins.bool | numpy.bool[builtins.bool] = ..., retall: Literal[True, 1], callback: Callable[[ndarray[tuple[int], dtype[float64]]], None] | None = ..., initial_simplex: CanArrayND[floating[Any] | integer[Any] | numpy.bool[builtins.bool], tuple[int, ...]] | Sequence[CanArrayND[floating[Any] | integer[Any] | numpy.bool[builtins.bool], tuple[int, ...]] | Sequence[float | floating[Any] | integer[Any] | numpy.bool[builtins.bool] | CanArray0D[floating[Any] | integer[Any] | numpy.bool[builtins.bool]]]] | None = ...) -> tuple[ndarray[tuple[int], dtype[float64]], float | floating[Any] | integer[Any] | numpy.bool[builtins.bool], int, int, Literal[0, 1, 2, 3, 4], list[ndarray[tuple[int], dtype[signedinteger[_32Bit | _64Bit]]] | ndarray[tuple[int], dtype[float64]]]]
- hydpy/auxs/ppolytools.py:253: error: Value of type variable "_AnyShapeType" of "__call__" of "_ConstructorEmpty" cannot be "tuple[int, signedinteger[_64Bit]]" [type-var]
- hydpy/auxs/ppolytools.py:253: error: Value of type variable "SupportsRichComparisonT" of "max" cannot be "signedinteger[_64Bit]" [type-var]
- hydpy/auxs/ppolytools.py:256: error: Incompatible types in assignment (expression has type "ndarray[tuple[int, signedinteger[_64Bit]], dtype[float64]]", variable has type "Sequence[Sequence[float] | ndarray[tuple[int, ...], dtype[float64]]] | ndarray[tuple[int, ...], dtype[float64]]") [assignment]
- hydpy/auxs/ppolytools.py:423: error: No overload variant of "CubicHermiteSpline" matches argument types "ndarray[tuple[int, ...], dtype[float64]]", "ndarray[tuple[int, ...], dtype[float64]]" [call-overload]
- hydpy/auxs/ppolytools.py:423: note: Possible overload variants:
- hydpy/auxs/ppolytools.py:423: note: def [_CT_co: float64 | complex128] __init__(self, x: CanArrayND[floating[Any] | integer[Any] | numpy.bool[builtins.bool], tuple[int, ...]] | Sequence[float | floating[Any] | integer[Any] | numpy.bool[builtins.bool] | CanArray0D[floating[Any] | integer[Any] | numpy.bool[builtins.bool]]], y: CanArrayND[floating[Any] | integer[Any] | numpy.bool[builtins.bool], tuple[int, ...]] | SequenceND[CanArrayND[floating[Any] | integer[Any] | numpy.bool[builtins.bool], tuple[int, ...]] | float | floating[Any] | integer[Any] | numpy.bool[builtins.bool] | CanArray0D[floating[Any] | integer[Any] | numpy.bool[builtins.bool]]], dydx: CanArrayND[floating[Any] | integer[Any] | numpy.bool[builtins.bool], tuple[int, ...]] | SequenceND[CanArrayND[floating[Any] | integer[Any] | numpy.bool[builtins.bool], tuple[int, ...]] | float | floating[Any] | integer[Any] | numpy.bool[builtins.bool] | CanArray0D[floating[Any] | integer[Any] | numpy.bool[builtins.bool]]], axis: int | integer[Any] = ..., extrapolate: Literal['periodic'] | bool | None = ...) -> CubicHermiteSpline[float64]
- hydpy/auxs/ppolytools.py:423: note: def [_CT_co: float64 | complex128] __init__(self, x: CanArrayND[floating[Any] | integer[Any] | numpy.bool[builtins.bool], tuple[int, ...]] | Sequence[float | floating[Any] | integer[Any] | numpy.bool[builtins.bool] | CanArray0D[floating[Any] | integer[Any] | numpy.bool[builtins.bool]]], y: CanArrayND[complexfloating[Any, Any], tuple[int, ...]] | SequenceND[CanArrayND[complexfloating[Any, Any], tuple[int, ...]] | JustComplex | complexfloating[Any, Any] | CanArray0D[complexfloating[Any, Any]]], dydx: CanArrayND[number[Any, int | float | complex] | numpy.bool[builtins.bool], tuple[int, ...]] | SequenceND[CanArrayND[number[Any, int | float | complex] | numpy.bool[builtins.bool], tuple[int, ...]] | complex | number[Any, int | float | complex] | numpy.bool[builtins.bool] | CanArray0D[number[Any, int | float | complex] | numpy.bool[builtins.bool]]], axis: int | integer[Any] = ..., extrapolate: Literal['periodic'] | bool | None = ...) -> CubicHermiteSpline[complex128]
- hydpy/auxs/ppolytools.py:423: note: def [_CT_co: float64 | complex128] __init__(self, x: CanArrayND[floating[Any] | integer[Any] | numpy.bool[builtins.bool], tuple[int, ...]] | Sequence[float | floating[Any] | integer[Any] | numpy.bool[builtins.bool] | CanArray0D[floating[Any] | integer[Any] | numpy.bool[builtins.bool]]], y: CanArrayND[number[Any, int | float | complex] | numpy.bool[builtins.bool], tuple[int, ...]] | SequenceND[CanArrayND[number[Any, int | float | complex] | numpy.bool[builtins.bool], tuple[int, ...]] | complex | number[Any, int | float | complex] | numpy.bool[builtins.bool] | CanArray0D[number[Any, int | float | complex] | numpy.bool[builtins.bool]]], dydx: CanArrayND[number[Any, int | float | complex] | numpy.bool[builtins.bool], tuple[int, ...]] | SequenceND[CanArrayND[number[Any, int | float | complex] | numpy.bool[builtins.bool], tuple[int, ...]] | complex | number[Any, int | float | complex] | numpy.bool[builtins.bool] | CanArray0D[number[Any, int | float | complex] | numpy.bool[builtins.bool]]], axis: int | integer[Any] = ..., extrapolate: Literal['periodic'] | bool | None = ...) -> CubicHermiteSpline[Any]
- hydpy/auxs/ppolytools.py:427: error: Incompatible types in assignment (expression has type "ndarray[tuple[int, ...], dtype[signedinteger[_64Bit]]]", variable has type "ndarray[tuple[int], dtype[signedinteger[_64Bit]]]") [assignment]
- hydpy/auxs/ppolytools.py:661: error: Value of type "float64" is not indexable [index]
- hydpy/auxs/armatools.py:226: error: No return value expected [return-value]
- hydpy/auxs/armatools.py:237: error: Argument 1 to "quad" has incompatible type "Callable[[Any, Any], None]"; expected "Callable[[float], float | floating[Any]] | Callable[[float64], float | floating[Any]] | LowLevelCallable[CapsuleType | PyCFuncPtr | _CFFIFuncPtr[_CFFIType, *tuple[_CFFIType, ...]] | CData, None]" [arg-type]
- hydpy/auxs/armatools.py:237: error: Argument "args" to "quad" has incompatible type "tuple[float]"; expected "tuple[()]" [arg-type]
- hydpy/auxs/armatools.py:258: error: Incompatible types in assignment (expression has type "ndarray[tuple[int, ...], dtype[Any]]", variable has type "ndarray[tuple[int], dtype[float64]]") [assignment]
- hydpy/models/rconc/rconc_control.py:422: error: Incompatible types in assignment (expression has type "ndarray[tuple[int, ...], Any]", variable has type "ndarray[tuple[int], dtype[floating[Any]]]") [assignment]
Diff from mypy_primer, showing the effect of this PR on open source code:
bokeh (https://github.com/bokeh/bokeh)
+ src/bokeh/models/util/structure.py:165: note: ... from here:
+ src/bokeh/models/util/structure.py:165: note: ... from here:
+ src/bokeh/models/util/structure.py:165: note: ... from here:
+ src/bokeh/models/util/structure.py:165: note: ... from here:
+ src/bokeh/models/util/structure.py:165: note: ... from here:
+ src/bokeh/models/util/structure.py:165: note: ... from here:
+ src/bokeh/models/util/structure.py:165: note: ... from here:
+ src/bokeh/models/util/structure.py:165: note: ... from here:
+ src/bokeh/models/util/structure.py:165: note: ... from here:
+ src/bokeh/models/util/structure.py:165: note: ... from here:
+ src/bokeh/models/util/structure.py:165: note: ... from here:
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉
Hi! I'm currently experimenting with a semi-automated way of extracting types from docstrings and applying them to stubs. Networkx was on my radar since the docstrings are very detailed and there is definitely some missing coverage.
I wanted to check in and see what I should do to avoid causing merge conflicts for you. Should I hold off till all of these changes are merged? Are there directories that you're done with that I can start looking at?
Hey @yangdanny97 ! These stubs are nothing more than https://github.com/microsoft/python-type-stubs/tree/ee6d039e893b5401e4b85c6229bafed3b984a941/stubs/networkx I don't trust their validity at all though (and rightfully so from what I've seen so far, part because some of it's obsolete, part because of what I'll describe below). So I've been extracting changes in chunks form this PR, and validating those.
These stubs were initially created using https://github.com/gramster/docs2stubs (which you may want to look into for docstring to stubs generation, I think stubgen is starting to have some support as well). Meaning networkx' own docstrings can be quite incorrect at times. Sometimes it's because they're taking shortcuts or using terms they're more familiar with despite being technically incorrect (labeling something a variable-length tuple when it could be any Iterable, labeling an Iterable a list, etc). Sometimes it's using a term that doesn't exist in the type system (for example Array, which may or may not be a sympy matrix or a numpy array). And sometimes it's just flat out wrong. Oh also sometimes the annotated type can differ from the docstring because it is transformed by a decorator. So you may end up with a lot of work/changes to do upstream to get accurate stubs generated from docstrings.
That being said, I'm working on this at a leisurely pace (and have other priorities atm with a big event in September). If we end up with conflicts that would simply mean I'd have to double-check the actual implementation. I wouldn't worry too much about that.
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉
Diff from mypy_primer, showing the effect of this PR on open source code:
discord.py (https://github.com/Rapptz/discord.py)
- discord/ext/commands/hybrid.py:834: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command" [misc]
+ discord/ext/commands/hybrid.py:834: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name" [misc]
- discord/ext/commands/hybrid.py:858: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command" [misc]
+ discord/ext/commands/hybrid.py:858: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name" [misc]
- discord/ext/commands/hybrid.py:883: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command" [misc]
+ discord/ext/commands/hybrid.py:883: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name" [misc]
- discord/ext/commands/hybrid.py:935: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command" [misc]
+ discord/ext/commands/hybrid.py:935: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name" [misc]
- discord/ext/commands/bot.py:290: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command" [misc]
+ discord/ext/commands/bot.py:290: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name" [misc]
- discord/ext/commands/bot.py:314: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command" [misc]
+ discord/ext/commands/bot.py:314: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name" [misc]
Diff from mypy_primer, showing the effect of this PR on open source code:
discord.py (https://github.com/Rapptz/discord.py)
- discord/ext/commands/hybrid.py:629: error: Overlap between argument names and ** TypedDict items: "name", "description" [misc]
+ discord/ext/commands/hybrid.py:629: error: Overlap between argument names and ** TypedDict items: "description", "name" [misc]
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉