Skip to content

Commit

Permalink
chore: Upgrade tests syntax.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanCardin committed Feb 26, 2024
1 parent 4bec953 commit c12622d
Show file tree
Hide file tree
Showing 34 changed files with 68 additions and 93 deletions.
10 changes: 5 additions & 5 deletions tests/arg/test_discriminated_union.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import Literal, Tuple, Union
from typing import Literal, Union

import pytest

Expand All @@ -13,9 +13,9 @@ def test_valid_tagged_unions(backend):
@dataclass
class ArgTest:
name: Union[
Tuple[Literal["one"], str],
Tuple[Literal["two"], int],
Tuple[
tuple[Literal["one"], str],
tuple[Literal["two"], int],
tuple[
Literal["three"],
float,
],
Expand All @@ -35,7 +35,7 @@ class ArgTest:
def test_disallowed_different_arity_variants(backend):
@dataclass
class ArgTest:
name: Union[Tuple[str, str], Tuple[str, str, str]]
name: Union[tuple[str, str], tuple[str, str, str]]

with pytest.raises(ValueError) as e:
parse(ArgTest, "one", "string", backend=backend)
Expand Down
3 changes: 1 addition & 2 deletions tests/arg/test_explicit_subcommand_types.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import Union

import cappa
from typing_extensions import Annotated
from typing_extensions import Annotated, Union

from tests.utils import backends, parse

Expand Down
3 changes: 1 addition & 2 deletions tests/arg/test_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import io
from contextlib import contextmanager
from dataclasses import dataclass
from typing import BinaryIO, TextIO
from unittest.mock import mock_open, patch

import cappa
import pytest
from typing_extensions import Annotated
from typing_extensions import Annotated, BinaryIO, TextIO

from tests.utils import backends, parse

Expand Down
2 changes: 1 addition & 1 deletion tests/arg/test_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import cappa
import pytest
from cappa.output import Exit
from typing_extensions import Annotated, Doc # type: ignore
from typing_extensions import Annotated, Doc

from tests.utils import backends, parse

Expand Down
21 changes: 12 additions & 9 deletions tests/arg/test_invalid_annotation_combination.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import List, Union

import cappa
import pytest
from typing_extensions import Annotated
from typing_extensions import Annotated, Union

from tests.utils import backends, parse

Expand All @@ -12,7 +13,7 @@
def test_sequence_unioned_with_scalar(backend):
@dataclass
class Args:
foo: Union[List[str], str]
foo: Union[list[str], str]

with pytest.raises(ValueError) as e:
parse(Args, "--help", backend=backend)
Expand All @@ -29,14 +30,15 @@ class Args:
def test_sequence_with_scalar_action(backend):
@dataclass
class Args:
foo: Annotated[List[str], cappa.Arg(action=cappa.ArgAction.set)]
foo: Annotated[list[str], cappa.Arg(action=cappa.ArgAction.set)]

with pytest.raises(ValueError) as e:
parse(Args, "--help", backend=backend)

assert str(e.value) == (
result = str(e.value).replace("typing.List", "list")
assert result == (
"On field 'foo', apparent mismatch of annotated type with `Arg` options. "
"'typing.List[str]' type produces a sequence, whereas `num_args=1`/`action=ArgAction.set` do not. "
"'list[str]' type produces a sequence, whereas `num_args=1`/`action=ArgAction.set` do not. "
"See [documentation](https://cappa.readthedocs.io/en/latest/annotation.html) for more details."
)

Expand All @@ -45,14 +47,15 @@ class Args:
def test_sequence_with_scalar_num_args(backend):
@dataclass
class Args:
foo: Annotated[List[str], cappa.Arg(num_args=1, short=True)]
foo: Annotated[list[str], cappa.Arg(num_args=1, short=True)]

with pytest.raises(ValueError) as e:
parse(Args, "--help", backend=backend)

assert str(e.value) == (
result = str(e.value).replace("typing.List", "list")
assert result == (
"On field 'foo', apparent mismatch of annotated type with `Arg` options. "
"'typing.List[str]' type produces a sequence, whereas `num_args=1`/`action=None` do not. "
"'list[str]' type produces a sequence, whereas `num_args=1`/`action=None` do not. "
"See [documentation](https://cappa.readthedocs.io/en/latest/annotation.html) for more details."
)

Expand Down
9 changes: 4 additions & 5 deletions tests/arg/test_list.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from __future__ import annotations

from dataclasses import dataclass, field
from typing import List, Union

import cappa
from typing_extensions import Annotated
from typing_extensions import Annotated, Union

from tests.utils import backends, parse

Expand All @@ -13,7 +12,7 @@
def test_list_option(backend):
@dataclass
class ArgTest:
variable_number: Annotated[List[str], cappa.Arg(short=True, long=True)] = field(
variable_number: Annotated[list[str], cappa.Arg(short=True, long=True)] = field(
default_factory=list
)

Expand All @@ -32,7 +31,7 @@ class ArgTest:
def test_list_positional(backend):
@dataclass
class ArgTest:
variable_number: List[str] = field(default_factory=list)
variable_number: list[str] = field(default_factory=list)

test = parse(
ArgTest,
Expand All @@ -49,7 +48,7 @@ def test_optional_list(backend):
@dataclass
class ArgTest:
value: Annotated[
Union[List[str], None], cappa.Arg(short=True, long=True)
Union[list[str], None], cappa.Arg(short=True, long=True)
] = None

test = parse(
Expand Down
9 changes: 4 additions & 5 deletions tests/arg/test_literal_sequence.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import List, Literal, Set, Tuple, Union

import cappa
import pytest
from typing_extensions import Annotated
from typing_extensions import Annotated, Literal, Union

from tests.utils import backends, parse


@dataclass
class ArgTest:
list: Annotated[
List[Union[Literal["one"], Literal["two"], Literal["three"], Literal[4]]],
list[Union[Literal["one"], Literal["two"], Literal["three"], Literal[4]]],
cappa.Arg(short=True, default=[]),
]
tuple: Annotated[
Tuple[Union[Literal["one"], Literal["two"], Literal["three"], Literal[4]], ...],
tuple[Union[Literal["one"], Literal["two"], Literal["three"], Literal[4]], ...],
cappa.Arg(short=True, default=()),
]
set: Annotated[
Set[Union[Literal["one"], Literal["two"], Literal["three"], Literal[4]]],
set[Union[Literal["one"], Literal["two"], Literal["three"], Literal[4]]],
cappa.Arg(short=True, default=set()),
]

Expand Down
3 changes: 1 addition & 2 deletions tests/arg/test_mapping_failure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

from dataclasses import dataclass
from pathlib import Path
from typing import Union

import cappa
import pytest
from typing_extensions import Annotated
from typing_extensions import Annotated, Union

from tests.utils import backends, parse

Expand Down
5 changes: 2 additions & 3 deletions tests/arg/test_parse.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import Tuple, Union

import cappa
import pytest
from typing_extensions import Annotated
from typing_extensions import Annotated, Union

from tests.utils import backends, parse

Expand Down Expand Up @@ -36,7 +35,7 @@ class ArgTest:
def test_not_typeable_parse_function(backend):
@dataclass
class ArgTest:
numbers: Annotated[Tuple[int, int], cappa.Arg(parse=split)]
numbers: Annotated[tuple[int, int], cappa.Arg(parse=split)]

test = parse(ArgTest, "1,3", backend=backend)
assert test.numbers == (1, 3)
Expand Down
3 changes: 1 addition & 2 deletions tests/arg/test_required.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from dataclasses import dataclass
from typing import Union

import cappa
import pytest
from typing_extensions import Annotated
from typing_extensions import Annotated, Union

from tests.utils import backends, parse

Expand Down
5 changes: 2 additions & 3 deletions tests/arg/test_set.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

from dataclasses import dataclass, field
from typing import Set

import cappa
from typing_extensions import Annotated
Expand All @@ -13,7 +12,7 @@
def test_list_option(backend):
@dataclass
class ArgTest:
variable_number: Annotated[Set[str], cappa.Arg(short=True, long=True)] = field(
variable_number: Annotated[set[str], cappa.Arg(short=True, long=True)] = field(
default_factory=set
)

Expand All @@ -36,7 +35,7 @@ class ArgTest:
def test_list_positional(backend):
@dataclass
class ArgTest:
variable_number: Set[int] = field(default_factory=set)
variable_number: set[int] = field(default_factory=set)

test = parse(
ArgTest,
Expand Down
3 changes: 1 addition & 2 deletions tests/arg/test_subcommand_required.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import Union

import cappa
import pytest
from typing_extensions import Annotated
from typing_extensions import Annotated, Union

from tests.utils import backends, parse

Expand Down
3 changes: 1 addition & 2 deletions tests/arg/test_tuple.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import Tuple

from tests.utils import backends, parse


@dataclass
class ArgTest:
numbers: Tuple[int, str, float]
numbers: tuple[int, str, float]


@backends
Expand Down
5 changes: 2 additions & 3 deletions tests/arg/test_tuple_unbounded.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import Tuple

import cappa
from typing_extensions import Annotated
Expand All @@ -13,7 +12,7 @@
def test_positional_arg(backend):
@dataclass
class ArgTest:
numbers: Tuple[int, ...]
numbers: tuple[int, ...]

test = parse(ArgTest, "1", "2", "3", "4", backend=backend)
assert test.numbers == (1, 2, 3, 4)
Expand All @@ -23,7 +22,7 @@ class ArgTest:
def test_option_flag(backend):
@dataclass
class ArgTest:
numbers: Annotated[Tuple[int, ...], cappa.Arg(short=True)]
numbers: Annotated[tuple[int, ...], cappa.Arg(short=True)]

test = parse(ArgTest, "-n", "1", "-n", "2", "-n", "3", "-n", "4", backend=backend)
assert test.numbers == (1, 2, 3, 4)
3 changes: 1 addition & 2 deletions tests/arg/test_value_name.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import Union

import cappa
from cappa import Subcommands
from typing_extensions import Annotated
from typing_extensions import Annotated, Union

from tests.utils import backends, parse

Expand Down
3 changes: 1 addition & 2 deletions tests/command/test_function_command.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import Union

import cappa
from typing_extensions import Annotated
from typing_extensions import Annotated, Union

from tests.utils import backends, invoke, parse

Expand Down
3 changes: 1 addition & 2 deletions tests/completion/test_choices.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import Literal, Union

import cappa
from typing_extensions import Annotated
from typing_extensions import Annotated, Literal, Union

from tests.utils import parse_completion

Expand Down
3 changes: 1 addition & 2 deletions tests/completion/test_subcommand.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import Union

import cappa
from typing_extensions import Annotated
from typing_extensions import Annotated, Union

from tests.utils import parse_completion

Expand Down
4 changes: 2 additions & 2 deletions tests/examples/test_kitchen_sink/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import logging
import sqlite3
from dataclasses import dataclass, field
from typing import List, Literal, Union
from typing import Literal, Union

import cappa
from typing_extensions import Annotated
Expand Down Expand Up @@ -39,7 +39,7 @@ class Example:

subcommand: Annotated[Union[MeowCommand, BarkCommand], cappa.Subcommand]

flags: Annotated[List[str], cappa.Arg(short=True, long=True)] = field(
flags: Annotated[list[str], cappa.Arg(short=True, long=True)] = field(
default_factory=list
)
flag: bool = False # --flag
Expand Down
Loading

0 comments on commit c12622d

Please sign in to comment.