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

Fix add_argparse_args raising TypeError with Python 3.6 #9554

Merged
merged 5 commits into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed `BasePredictionWriter` not returning the batch_indices in a non-distributed setting ([#9432](https://github.com/PyTorchLightning/pytorch-lightning/pull/9432))


- Fixed `add_argparse_args` raising `TypeError` when args are typed as `typing.Generic` in Python 3.6 ([#9554](https://github.com/PyTorchLightning/pytorch-lightning/pull/9554))


## [1.4.7] - 2021-09-14

- Fixed logging of nan parameters ([#9364](https://github.com/PyTorchLightning/pytorch-lightning/pull/9364))
Expand Down
2 changes: 1 addition & 1 deletion pytorch_lightning/utilities/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def get_init_arguments_and_types(cls: Any) -> List[Tuple[str, Tuple, Any]]:
arg_default = cls_default_params[arg].default
try:
arg_types = tuple(arg_type.__args__)
except AttributeError:
except (AttributeError, TypeError):
arg_types = (arg_type,)

name_type_default.append((arg, arg_types, arg_default))
Expand Down
18 changes: 17 additions & 1 deletion tests/utilities/test_argparse.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import io
from argparse import ArgumentParser, Namespace
from typing import List
from typing import Generic, List, TypeVar
from unittest.mock import MagicMock

import pytest
Expand Down Expand Up @@ -137,6 +137,16 @@ def __init__(self, my_parameter: int = 0):
pass


class AddArgparseArgsExampleClassGeneric:
T = TypeVar("T")

class SomeClass(Generic[T]):
pass

def __init__(self, invalid_class: SomeClass):
pass


def extract_help_text(parser):
help_str_buffer = io.StringIO()
parser.print_help(file=help_str_buffer)
Expand Down Expand Up @@ -203,6 +213,12 @@ def test_add_argparse_args_no_argument_group():
assert args.my_parameter == 2


def test_add_argparse_args_invalid():
"""Test that `add_argparse_args` doesn't raise `TypeError` when a class has args typed as `typing.Generic` in
Python 3.6."""
add_argparse_args(AddArgparseArgsExampleClassGeneric, ArgumentParser())


def test_gpus_allowed_type():
assert _gpus_allowed_type("1,2") == "1,2"
assert _gpus_allowed_type("1") == 1
Expand Down