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

Minimal tensorflow stub structure #7319

Closed

Conversation

hmc-cs-mdrissi
Copy link
Contributor

@hmc-cs-mdrissi hmc-cs-mdrissi commented Feb 20, 2022

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.

from builtins import bool as _bool
from typing import Any, Iterable, Iterator, NoReturn, overload

import numpy as np
Copy link
Contributor Author

@hmc-cs-mdrissi hmc-cs-mdrissi Feb 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"]]
Copy link
Contributor Author

@hmc-cs-mdrissi hmc-cs-mdrissi Feb 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@hmc-cs-mdrissi hmc-cs-mdrissi marked this pull request as draft February 20, 2022 11:17
@hmc-cs-mdrissi
Copy link
Contributor Author

Marking as draft, as it looks like step 1 is adding support for non-stub dependencies.

@hmc-cs-mdrissi
Copy link
Contributor Author

I will close this for now and reopen it later once I've gotten CI worked out for using numpy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant