|
1 | 1 | # log21.Argparse.py |
2 | 2 | # CodeWriter21 |
3 | 3 |
|
| 4 | +from __future__ import annotations |
| 5 | + |
4 | 6 | import re as _re |
5 | 7 | import sys as _sys |
6 | 8 | import types as _types |
7 | 9 | import typing as _typing |
8 | 10 | import argparse as _argparse |
9 | 11 | from enum import Enum as _Enum |
| 12 | +from typing import (Tuple as _Tuple, Mapping as _Mapping, Optional as _Optional, |
| 13 | + Sequence as _Sequence) |
10 | 14 | from gettext import gettext as _gettext |
11 | 15 | from textwrap import TextWrapper as _TextWrapper |
12 | 16 | from collections import OrderedDict as _OrderedDict |
13 | | -from typing import ( |
14 | | - Mapping as _Mapping, Optional as _Optional, Tuple as _Tuple, Sequence as _Sequence |
15 | | -) |
16 | 17 |
|
17 | 18 | import log21 as _log21 |
18 | 19 | from log21.Colors import get_colors as _gc |
@@ -485,11 +486,13 @@ def _wrap_chunks(self, chunks): # noqa: C901 |
485 | 486 | del chunks[-1] |
486 | 487 |
|
487 | 488 | while chunks: |
488 | | - # modified upstream code, not going to refactor for ambiguous variable name. |
| 489 | + # modified upstream code, not going to refactor for ambiguous variable |
| 490 | + # name. |
489 | 491 | length = len(_Formatter.decolorize(chunks[-1])) # noqa: E741 |
490 | 492 |
|
491 | 493 | # Can at least squeeze this chunk onto the current line. |
492 | | - # Modified upstream code, not going to refactor for ambiguous variable name. |
| 494 | + # Modified upstream code, not going to refactor for ambiguous variable |
| 495 | + # name. |
493 | 496 | if current_len + length <= width: # noqa: E741 |
494 | 497 | current_line.append(chunks.pop()) |
495 | 498 | current_len += length |
@@ -545,35 +548,38 @@ class _ActionsContainer(_argparse._ActionsContainer): |
545 | 548 | # pylint: disable=too-many-branches |
546 | 549 | def _validate_func_type(self, action, func_type, kwargs, level: int = 0) -> _Tuple: |
547 | 550 | # raise an error if the action type is not callable |
548 | | - if not callable(func_type) and not isinstance(func_type, |
549 | | - (_types.UnionType, tuple)): |
| 551 | + if (hasattr(_types, 'UnionType') and not callable(func_type) |
| 552 | + and not isinstance(func_type, (_types.UnionType, tuple))): |
550 | 553 | raise ValueError(f'{func_type} is not callable; level={level}') |
551 | 554 |
|
552 | 555 | # Handle `UnionType` as a type (e.g. `int|str`) |
553 | | - if isinstance(func_type, _types.UnionType): |
| 556 | + if hasattr(_types, 'UnionType') and isinstance(func_type, _types.UnionType): |
554 | 557 | func_type = func_type.__args__ # type: ignore |
555 | 558 |
|
556 | 559 | # Handle `Literal` as a type (e.g. `Literal[1, 2, 3]`) |
557 | | - elif isinstance(func_type, _typing._LiteralGenericAlias): |
| 560 | + elif (hasattr(_typing, '_LiteralGenericAlias') |
| 561 | + and isinstance(func_type, _typing._LiteralGenericAlias)): # type: ignore |
558 | 562 | func_type = Literal(func_type) |
559 | 563 |
|
560 | 564 | # Handle `List` as a type (e.g. `List[int]`) |
561 | | - elif isinstance(func_type, |
562 | | - _typing._GenericAlias) and func_type.__origin__ is list: |
| 565 | + elif (hasattr(_typing, '_GenericAlias') |
| 566 | + and isinstance(func_type, _typing._GenericAlias) # type: ignore |
| 567 | + and func_type.__origin__ is list): |
563 | 568 | func_type = func_type.__args__[0] |
564 | 569 | if kwargs.get('nargs') is None: |
565 | 570 | action.nargs = '+' |
566 | 571 |
|
567 | 572 | # Handle `Required` as a type (e.g. `Required[int]`) |
568 | | - elif isinstance( |
569 | | - func_type, |
570 | | - _typing._GenericAlias) and func_type.__origin__ is _typing.Required: |
| 573 | + elif (hasattr(_typing, 'Required') and hasattr(_typing, '_GenericAlias') |
| 574 | + and isinstance(func_type, _typing._GenericAlias) # type: ignore |
| 575 | + and func_type.__origin__ is _typing.Required): |
571 | 576 | func_type = func_type.__args__[0] |
572 | 577 | action.required = True |
573 | 578 |
|
574 | 579 | # Handle `Union` and `Optional` as a type (e.g. `Union[int, str]` and |
575 | 580 | # `Optional[int]`) |
576 | | - elif isinstance(func_type, _typing._UnionGenericAlias): |
| 581 | + elif (hasattr(_types, 'NoneType') and hasattr(_typing, '_UnionGenericAlias') |
| 582 | + and isinstance(func_type, _typing._UnionGenericAlias)): # type: ignore |
577 | 583 | # Optional[T] is just Union[T, NoneType] |
578 | 584 | # Optional |
579 | 585 | if (len(func_type.__args__) == 2 |
@@ -620,12 +626,16 @@ def _validate_func_type(self, action, func_type, kwargs, level: int = 0) -> _Tup |
620 | 626 | temp.extend(self._validate_func_type(action, type_, kwargs, level + 1)) |
621 | 627 | func_type = tuple(temp) |
622 | 628 | else: |
623 | | - if isinstance(func_type, ( |
624 | | - _typing._GenericAlias, |
625 | | - _typing._UnionGenericAlias, |
626 | | - _typing._LiteralGenericAlias, |
627 | | - _types.UnionType, |
628 | | - )): |
| 629 | + if (hasattr(_types, 'UnionType') and hasattr(_typing, '_GenericAlias') |
| 630 | + and hasattr(_typing, '_UnionGenericAlias') |
| 631 | + and hasattr(_typing, '_LiteralGenericAlias') and isinstance( |
| 632 | + func_type, |
| 633 | + ( |
| 634 | + _typing._GenericAlias, # type: ignore |
| 635 | + _typing._UnionGenericAlias, # type: ignore |
| 636 | + _typing._LiteralGenericAlias, # type: ignore |
| 637 | + _types.UnionType, |
| 638 | + ))): |
629 | 639 | func_type = self._validate_func_type( |
630 | 640 | action, func_type, kwargs, level + 1 |
631 | 641 | ) |
@@ -744,8 +754,7 @@ def _get_formatter(self): |
744 | 754 | return self.formatter_class(prog=self.prog) |
745 | 755 |
|
746 | 756 | def _get_value(self, action, arg_string): |
747 | | - """Override _get_value to add support for types such as Union and |
748 | | - Literal.""" |
| 757 | + """Override _get_value to add support for types such as Union and Literal.""" |
749 | 758 |
|
750 | 759 | func_type = self._registry_get('type', action.type, action.type) |
751 | 760 | if not callable(func_type) and not isinstance(func_type, tuple): |
@@ -1079,7 +1088,8 @@ def consume_positionals(start_index): |
1079 | 1088 | if required_actions: |
1080 | 1089 | self.error( |
1081 | 1090 | _gettext( |
1082 | | - f'the following arguments are required: {", ".join(required_actions)}' |
| 1091 | + 'the following arguments are required: ' + |
| 1092 | + ", ".join(required_actions) |
1083 | 1093 | ) |
1084 | 1094 | ) |
1085 | 1095 |
|
|
0 commit comments