Skip to content

fix: correct __all__ names in classification/loss modules - #123

Open
shivamlalakiya wants to merge 1 commit into
ml-stat-Sustech:masterfrom
shivamlalakiya:fix/loss-all-names-mismatch
Open

shivamlalakiya wants to merge 1 commit into
ml-stat-Sustech:masterfrom
shivamlalakiya:fix/loss-all-names-mismatch

Conversation

@shivamlalakiya

Copy link
Copy Markdown

What

conftr.py, confts.py and scpo.py each declare an __all__ that names a
class that does not exist in the module:

classification/loss/conftr.py:8   __all__ = ["ConfTr"]   # class defined is ConfTrLoss
classification/loss/confts.py:8   __all__ = ["ConfTS"]   # class defined is ConfTSLoss
classification/loss/scpo.py:8     __all__ = ["SCPO"]     # class defined is SCPOLoss

A wildcard import from any of the three modules directly raises
AttributeError instead of binding the class. Reproduced against the
1.2.1 wheel from PyPI:

>>> from torchcp.classification.loss.conftr import *
AttributeError: module 'torchcp.classification.loss.conftr' has no attribute 'ConfTr'
>>> from torchcp.classification.loss.confts import *
AttributeError: module 'torchcp.classification.loss.confts' has no attribute 'ConfTS'
>>> from torchcp.classification.loss.scpo import *
AttributeError: module 'torchcp.classification.loss.scpo' has no attribute 'SCPO'

The named-import path is unaffected, because torchcp/classification/loss/__init__.py
imports the real class names directly rather than going through __all__:

>>> from torchcp.classification.loss import ConfTrLoss, ConfTSLoss, SCPOLoss
# ok

So this only bites a caller who does from torchcp.classification.loss.conftr import *
(or the confts / scpo equivalents) instead of importing from the package.

Fix

One line changed per file: __all__ now names the class that is actually
defined (ConfTrLoss, ConfTSLoss, SCPOLoss). Verified the wildcard import
resolves correctly against the patched files:

>>> from torchcp.classification.loss.conftr import *; ConfTrLoss
<class 'torchcp.classification.loss.conftr.ConfTrLoss'>
>>> from torchcp.classification.loss.confts import *; ConfTSLoss
<class 'torchcp.classification.loss.confts.ConfTSLoss'>
>>> from torchcp.classification.loss.scpo import *; SCPOLoss
<class 'torchcp.classification.loss.scpo.SCPOLoss'>

This is a pure one-liner per file, no other behaviour changes.

conftr.py, confts.py and scpo.py declare __all__ = ["ConfTr"],
["ConfTS"] and ["SCPO"], but the classes defined in those files are
ConfTrLoss, ConfTSLoss and SCPOLoss. A wildcard import from any of the
three modules raises AttributeError instead of binding the class:

>>> from torchcp.classification.loss.conftr import *
AttributeError: module 'torchcp.classification.loss.conftr' has no
attribute 'ConfTr'

Named imports (from torchcp.classification.loss import ConfTrLoss) are
unaffected since the package __init__ imports the real names. This
updates __all__ in each file to the actual class name.
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