Skip to content

Conversation

@scotts
Copy link
Contributor

@scotts scotts commented Nov 3, 2025

Fixes #1017

Curiously, I tried writing a unit test for this, but it passed without making any changes. :/ So there's something with how we're importing things during test that is making torchcodec.encoders available even though it's not exposed in the top-level __init__.py.

However, when I loaded up an interactive python shell without this change, I got:

>>> import torchcodec
>>> import torch
>>> encoder = torchcodec.encoders.AudioEncoder(samples=torch.rand(1), sample_rate=16_000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'torchcodec' has no attribute 'encoders'. Did you mean: 'decoders'?

Same as the user. And then with this change:

>>> import torchcodec
>>> import torch
>>> encoder = torchcodec.encoders.AudioEncoder(samples=torch.rand(1), sample_rate=16_000)
>>> 

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 3, 2025
@scotts scotts marked this pull request as ready for review November 3, 2025 19:50
Copy link
Contributor

@Dan-Flores Dan-Flores left a comment

Choose a reason for hiding this comment

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

This is interesting, there is a subtle difference between how we import in tests: from torchcodec.encoders import AudioEncoder and how one can import in python: encoder = torchcodec.encoders.AudioEncoder(...)

Copy link
Contributor

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

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

So there's something with how we're importing things during test that is making torchcodec.encoders available even though it's not exposed in the top-level init.py.

That's because torchcodec.encoders becomes available if we do something like from torchcodec.encoders import AudioEncoder (which we do everywhere in the tests):

In [1]: import torchcodec

In [3]: torchcodec.encoders  # not available
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[3], line 1
----> 1 torchcodec.encoders

AttributeError: module 'torchcodec' has no attribute 'encoders'

In [4]: from torchcodec.encoders import AudioEncoder

In [5]: torchcodec.encoders  # now available
Out[5]: <module 'torchcodec.encoders' from '/home/nicolashug/dev/torchcodec/src/torchcodec/encoders/__init__.py'>

If we really wanted to test the import, we'd have to spawn a separate "virgin" process whos imports aren't poluted. E.g. like in https://github.com/scikit-learn/scikit-learn/blob/eee6582df6f64678c81209037d3800b4f319fb88/sklearn/experimental/tests/test_enable_hist_gradient_boosting.py. Probably overkill.

@scotts scotts merged commit c552b60 into main Nov 4, 2025
59 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Why not exposing encoders module

4 participants