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

add_argparse_args raises TypeError with Python 3.6 when class has arg typed Generic #9535

Closed
akihironitta opened this issue Sep 15, 2021 · 1 comment · Fixed by #9554
Closed
Labels
bug Something isn't working data handling Generic data-related topic help wanted Open to be worked on

Comments

@akihironitta
Copy link
Contributor

🐛 Bug

When a LightningDataModule has args typed torch.utils.data.Dataset, it throws TypeError.

possible cause
As far as I investigated, it seems like the error occurs only when a class has typing.Generic-typed args (or any subclass of typing.Generic, e.g. torch.utils.data.Dataset). Note that it doesn't occur when args are typed as torch.nn.Module, for example.

Since typing.GenericMeta is removed in 3.7 (PEP 560), the error doesn't occur in Python 3.7 or later.

# Python 3.6
>>> torch.utils.data.Dataset, type(torch.utils.data.Dataset)
(torch.utils.data.dataset.Dataset, <class 'typing.GenericMeta'>)
>>> torch.nn.Module, type(torch.nn.Module)
(<class 'torch.nn.modules.module.Module'>, <class 'type'>)

# Python 3.9
>>> torch.utils.data.Dataset, type(torch.utils.data.Dataset)
(<class 'torch.utils.data.dataset.Dataset'>, <class 'type'>)
>>> torch.nn.Module, type(torch.nn.Module)
(<class 'torch.nn.modules.module.Module'>, <class 'type'>)

To Reproduce

from argparse import ArgumentParser
import torch
import pytorch_lightning as pl

class MyDataModule(pl.LightningDataModule):
    def  __init__(self, some_arg: torch.utils.data.Dataset):
        pass

def test_invalid_add_argparse_args():
    MyDataModule.add_argparse_args(ArgumentParser())
    
test_invalid_add_argparse_args()
Traceback (most recent call last):
  File "/home/nitta/tmp/test_argparse.py", line 12, in <module>
    test_invalid_add_argparse_args()
  File "/home/nitta/tmp/test_argparse.py", line 10, in test_invalid_add_argparse_args
    MyDataModule.add_argparse_args(ArgumentParser())
  File "/home/nitta/work/github.com/PyTorchLightning/pytorch-lightning/pytorch_lightning/core/datamodule.py", line 338, in add_argparse_args
    return add_argparse_args(cls, parent_parser, **kwargs)
  File "/home/nitta/work/github.com/PyTorchLightning/pytorch-lightning/pytorch_lightning/utilities/argparse.py", line 217, in add_argparse_args
    args_and_types = get_init_arguments_and_types(symbol)
  File "/home/nitta/work/github.com/PyTorchLightning/pytorch-lightning/pytorch_lightning/utilities/argparse.py", line 141, in get_init_arguments_and_types
    arg_types = tuple(arg_type.__args__)
TypeError: 'NoneType' object is not iterable

Expected behavior

.add_argparse_args doesn't raise TypeError and just ignores class args typed Generic using Python 3.6.

Environment

  • CUDA:
    • GPU:
    • available: False
    • version: 10.2
  • Packages:
    • numpy: 1.19.5
    • pyTorch_debug: False
    • pyTorch_version: 1.9.0+cu102
    • pytorch-lightning: 1.5.0dev
    • tqdm: 4.62.2
  • System:
    • OS: Linux
    • architecture:
      • 64bit
    • processor:
    • python: 3.6.13
    • version: Proposal for help #1 SMP Debian 4.19.194-3 (2021-07-18)

Additional context

This error was found while handling Lightning-Universe/lightning-bolts#466.

@akihironitta akihironitta added bug Something isn't working help wanted Open to be worked on data handling Generic data-related topic labels Sep 15, 2021
@akihironitta
Copy link
Contributor Author

A fix should could be straight-forward as the following:

-         except AttributeError:
+         except (AttributeError, TypeError):

https://github.com/PyTorchLightning/pytorch-lightning/blob/637f59f1d2caaad57af3ddfa05b1089e6da6d3d0/pytorch_lightning/utilities/argparse.py#L142

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working data handling Generic data-related topic help wanted Open to be worked on
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant