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

Consider partial stubs for TensorFlow to work around lazy import issues #3174

Closed
gramster opened this issue Aug 10, 2022 · 6 comments
Closed
Labels
needs decision Do we want this enhancement?

Comments

@gramster
Copy link
Member

TF does a bunch of nasty stuff to implement lazy loading of modules, which means we don't pick up the imports when doing static analysis. We should see if we can use a partial stub that just uses (non-lazy) imports to work around this problem.

@hmc-cs-mdrissi
Copy link

hmc-cs-mdrissi commented Aug 10, 2022

Hmm, tf imports are a lot worse then that. Tensorflow uses a decorator to modify expected import paths at runtime heavily. Most tensorflow classes look like,

@tf_export("data.Dataset", v1=[])
class Dataset:
  ... 

And you import Dataset from tf.data even though it's actually defined in tf.python.data.ops.dataset_ops. tf.data folder doesn't even exist in source files. Recommending imports like tf.python.data.ops is a good way to have a lot of internal imports that are undocumented/may be unstable across versions.

My experience with tf stubs is manually creating stubs based on documentation and analyzing source it does too much import magic unless you want to teach tool to know how tf_export decorator behaves. I've tried generated stubs + adjustments before (both stubgen and pyright --createstub) and got too many false positives and gave up on them.

I have a decent amount of tf stubs but have procrastinated upstreaming them to typeshed (mostly need to figure out this first)

@gramster
Copy link
Member Author

gramster commented Aug 11, 2022 via email

@hmc-cs-mdrissi
Copy link

Sure, I've made a pr with all my current tensorflow stubs. We can use that to discuss if those stubs are worth having there. It's a bit unclear to me on what place they should eventually live in, but somewhere open source that others can contribute is better then nowhere.

@judej judej added the needs decision Do we want this enhancement? label Aug 11, 2022
@Avasam
Copy link

Avasam commented Jan 5, 2023

@hmc-cs-mdrissi
typeshed is now very close to allowing non-types dependencies (there's a small issue with pytype left to fix, but pyright and stubtest work).

@hmc-cs-mdrissi
Copy link

Yes I'm happy to see your/Alex's work. I'll pick back up the tensorflow initial stubs pr this weekend and see if numpy errors are now all resolved.

@hmc-cs-mdrissi
Copy link

I think this can be closed. types-tensorflow now exists. It is very incomplete and will grow over time, but anyone is welcome to add more stubs to typeshed and I'll happily review them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs decision Do we want this enhancement?
Projects
None yet
Development

No branches or pull requests

5 participants