Skip to content

Commit 9d2faf6

Browse files
fix: Improved compatibility.
1 parent 8c3e7e2 commit 9d2faf6

8 files changed

Lines changed: 273 additions & 222 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ Help this project by [Donation](DONATE.md)
55

66
Changes
77
-----------
8+
### 2.7.1
9+
10+
+ Improved compatibility
811

912
### 2.7.0
1013

README.md

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,9 @@ pip install git+https://github.com/MPCodeWriter21/log21
6161
Changes
6262
-------
6363

64-
### 2.7.0
65-
66-
+ Modified `automatic-release.yml` and `pypi.yml` workflows to check the
67-
version.
68-
+ Added the support for more `type`s to pass to
69-
`ColorizingArgumentParser().add_argument(...)`: `typing.Union`, `typing.Optional`,
70-
`typing.Literal`, `enum.Enum`, `tuple` and `typing.Required`.
71-
+ Modified the way `Enum`s are handled in the Argument Parser.
72-
+ Handled some `typing._SpecialForm`s.
73-
+ A normal ArgumentGroup can now be required! (Unlike MutuallyExclusiveGroup it can
74-
have more than 1 option used at the same time)
75-
+ `argumentify` now supports async functions as the entry point.
64+
### 2.7.1
65+
66+
+ Improved compatibility
7667

7768
[Full CHANGELOG](https://github.com/MPCodeWriter21/log21/blob/master/CHANGELOG.md)
7869

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ dependencies = [
2626
"webcolors",
2727
"docstring-parser"
2828
]
29-
version = "2.7.0"
29+
version = "2.7.1"
3030

3131
[tool.setuptools.packages.find]
3232
where = ["src"]

src/log21/Argparse.py

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
# log21.Argparse.py
22
# CodeWriter21
33

4+
from __future__ import annotations
5+
46
import re as _re
57
import sys as _sys
68
import types as _types
79
import typing as _typing
810
import argparse as _argparse
911
from enum import Enum as _Enum
12+
from typing import (Tuple as _Tuple, Mapping as _Mapping, Optional as _Optional,
13+
Sequence as _Sequence)
1014
from gettext import gettext as _gettext
1115
from textwrap import TextWrapper as _TextWrapper
1216
from collections import OrderedDict as _OrderedDict
13-
from typing import (
14-
Mapping as _Mapping, Optional as _Optional, Tuple as _Tuple, Sequence as _Sequence
15-
)
1617

1718
import log21 as _log21
1819
from log21.Colors import get_colors as _gc
@@ -485,11 +486,13 @@ def _wrap_chunks(self, chunks): # noqa: C901
485486
del chunks[-1]
486487

487488
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.
489491
length = len(_Formatter.decolorize(chunks[-1])) # noqa: E741
490492

491493
# 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.
493496
if current_len + length <= width: # noqa: E741
494497
current_line.append(chunks.pop())
495498
current_len += length
@@ -545,35 +548,38 @@ class _ActionsContainer(_argparse._ActionsContainer):
545548
# pylint: disable=too-many-branches
546549
def _validate_func_type(self, action, func_type, kwargs, level: int = 0) -> _Tuple:
547550
# 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))):
550553
raise ValueError(f'{func_type} is not callable; level={level}')
551554

552555
# 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):
554557
func_type = func_type.__args__ # type: ignore
555558

556559
# 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
558562
func_type = Literal(func_type)
559563

560564
# 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):
563568
func_type = func_type.__args__[0]
564569
if kwargs.get('nargs') is None:
565570
action.nargs = '+'
566571

567572
# 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):
571576
func_type = func_type.__args__[0]
572577
action.required = True
573578

574579
# Handle `Union` and `Optional` as a type (e.g. `Union[int, str]` and
575580
# `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
577583
# Optional[T] is just Union[T, NoneType]
578584
# Optional
579585
if (len(func_type.__args__) == 2
@@ -620,12 +626,16 @@ def _validate_func_type(self, action, func_type, kwargs, level: int = 0) -> _Tup
620626
temp.extend(self._validate_func_type(action, type_, kwargs, level + 1))
621627
func_type = tuple(temp)
622628
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+
))):
629639
func_type = self._validate_func_type(
630640
action, func_type, kwargs, level + 1
631641
)
@@ -744,8 +754,7 @@ def _get_formatter(self):
744754
return self.formatter_class(prog=self.prog)
745755

746756
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."""
749758

750759
func_type = self._registry_get('type', action.type, action.type)
751760
if not callable(func_type) and not isinstance(func_type, tuple):
@@ -1079,7 +1088,8 @@ def consume_positionals(start_index):
10791088
if required_actions:
10801089
self.error(
10811090
_gettext(
1082-
f'the following arguments are required: {", ".join(required_actions)}'
1091+
'the following arguments are required: ' +
1092+
", ".join(required_actions)
10831093
)
10841094
)
10851095

src/log21/CrashReporter/Reporters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,10 @@ class EmailReporter(Reporter): # pylint: disable=too-many-instance-attributes
322322
>>> divide(10, 0)
323323
Traceback (most recent call last):
324324
File "<stdin>", line 1, in <module>
325-
File "%localappdata%\\Programs\\Python\\Python310\\lib\\site-packages\\log21\\CrashReporter\\Reporters.py",
325+
File ".../site-packages/log21/CrashReporter/Reporters.py",
326326
line 81, in wrap
327327
raise e
328-
File "%localappdata%\\Programs\\Python\\Python310\\lib\\site-packages\\log21\\CrashReporter\\Reporters.py",
328+
File ".../site-packages/log21/CrashReporter/Reporters.py",
329329
line 77, in wrap
330330
return func(*args, **kwargs)
331331
File "<stdin>", line 3, in divide

0 commit comments

Comments
 (0)