Skip to content

Commit

Permalink
Fix typedict generic typing for old pythons
Browse files Browse the repository at this point in the history
TypedDicts and Generics can only be mixed in py311+. Add comptibility
class for older pythons
  • Loading branch information
pvandyken committed Apr 17, 2024
1 parent ce4cdba commit 421e8a3
Showing 1 changed file with 40 additions and 16 deletions.
56 changes: 40 additions & 16 deletions snakebids/plugins/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import argparse
import sys
from typing import (
TYPE_CHECKING,
Any,
Expand All @@ -12,28 +13,51 @@
overload,
)

from typing_extensions import Required, Unpack
from typing_extensions import Required, TypeAlias, Unpack

if TYPE_CHECKING:
from argparse import _SUPPRESS_T, _ActionStr, _ArgumentGroup, _NArgsStr

_T = TypeVar("_T")


class AddArgumentArgs(TypedDict, Generic[_T], total=False):
"""Arguments for add_argument, with ``dest`` required."""
if sys.version_info >= (3, 11):

action: _ActionStr | type[argparse.Action]
nargs: int | _NArgsStr | _SUPPRESS_T | None
const: Any
default: Any
type: Callable[[str], _T] | argparse.FileType
choices: Iterable[_T] | None
required: bool
help: str | None
metavar: str | tuple[str, ...] | None
dest: Required[str]
version: str
class AddArgumentArgs(TypedDict, Generic[_T], total=False):
"""Arguments for add_argument, with ``dest`` required."""

action: _ActionStr | type[argparse.Action]
nargs: int | _NArgsStr | _SUPPRESS_T | None
const: Any
default: Any
type: Callable[[str], _T] | argparse.FileType
choices: Iterable[_T] | None
required: bool
help: str | None
metavar: str | tuple[str, ...] | None
dest: Required[str]
version: str

AnyArgumentArgs: TypeAlias = "AddArgumentArgs[Any]"

else:

class AddArgumentArgs(TypedDict, total=False):
"""Arguments for add_argument, with ``dest`` required."""

action: _ActionStr | type[argparse.Action]
nargs: int | _NArgsStr | _SUPPRESS_T | None
const: Any
default: Any
type: Callable[[str], Any] | argparse.FileType
choices: Iterable[Any] | None
required: bool
help: str | None
metavar: str | tuple[str, ...] | None
dest: Required[str]
version: str

AnyArgumentArgs: TypeAlias = "AddArgumentArgs"


class PluginBase:
Expand Down Expand Up @@ -75,7 +99,7 @@ def try_add_argument(
self,
parser: argparse.ArgumentParser | _ArgumentGroup,
*name_or_flags: str,
**kwargs: Unpack[AddArgumentArgs[Any]],
**kwargs: Unpack[AnyArgumentArgs],
) -> argparse.Action | None:
"""Add argument to parser if provided dest is not already present."""
if self.PREFIX and not kwargs["dest"].startswith(self.PREFIX):
Expand All @@ -88,7 +112,7 @@ def add_argument(
self,
parser: argparse.ArgumentParser | _ArgumentGroup,
*name_or_flags: str,
**kwargs: Unpack[AddArgumentArgs[Any]],
**kwargs: Unpack[AnyArgumentArgs],
) -> argparse.Action | None:
"""Add argument to parser, applying prefix to dest as necesary."""
if self.PREFIX and not kwargs["dest"].startswith(self.PREFIX):
Expand Down

0 comments on commit 421e8a3

Please sign in to comment.