Repository navigation
Minimal tensorflow stub structure - #7319
hmc-cs-mdrissi wants to merge 3 commits into
Conversation
| from builtins import bool as _bool | ||
| from typing import Any, Iterable, Iterator, NoReturn, overload | ||
|
|
||
| import numpy as np |
There was a problem hiding this comment.
How do I handle numpy errors? I thought requires line in METADATA.toml would allow using numpy?
numpy is only library I think needed as most tensorflow functions accept numpy arrays too.
I think all of unknown pyright errors are about np.ndarray/np.number.
edit: @jakebailey Any advice on pyright action and handling a stub package that depends on a separate python package? Would it be best have numpy installed as part of pyright check, add this folder to exclude list for pyrightconfig.json, or something else?
|
|
||
| # These aliases mostly ignore rank/shape/dtype information as that | ||
| # will complicate the types heavily and can be a follow up problem. | ||
| _FloatDataSequence = Union[Sequence[float], Sequence["_FloatDataSequence"]] |
There was a problem hiding this comment.
Several of these type aliases are recursive. What would be recommended way to write them? Arbitrarily nested sequences or json like containers (nested maps/sequences) are very common types needed. Should I leave them like this, use a recursive protocol, avoid recursion and fallback to Any beyond given depth, or something else?
_ContainerGeneric and _DataSequence alises are the main recursive ones here.
edit: It looks like recursion produces this error for pytype,
stubs/tensorflow/tensorflow/init.pyi (3.9): ParseError: Sequence['_FloatDataSequence'] not supported
I'm surprised mypy passes but I think mypy treats recursive aliases like Any currently.
|
Marking as draft, as it looks like step 1 is adding support for non-stub dependencies. |
|
I will close this for now and reopen it later once I've gotten CI worked out for using numpy. |
Summary
Minimal pr to start tensorflow stubs.
Due to tensorflow's heavy import magic using stubgen to make all files isn't really an option here. Defining the right files to even make stubs for is non-trivial as most classes in tensorflow are defined in private file and then dynamically exported elsewhere (sometimes in ways not describable by stubs). So I expect partial packages/modules to be norm for these stubs for a while.
I included basic stub for most common class Tensor, one example simple function in math.pyi, and commonly needed type aliases.
edit: I end up having two follow prs be added here as separate commits. I can move them to separate prs, I'm unsure if prs are allowed to point to a different base from a separate fork though.