From 333bbf630164a782df9f375f6d343d80704f1711 Mon Sep 17 00:00:00 2001 From: mbaruh Date: Mon, 27 Feb 2023 22:09:46 +0200 Subject: Use public typing API for `starting_value` Co-authored-by: Ionite --- bot/exts/filtering/_utils.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/bot/exts/filtering/_utils.py b/bot/exts/filtering/_utils.py index 5abb2240b..b96767fbf 100644 --- a/bot/exts/filtering/_utils.py +++ b/bot/exts/filtering/_utils.py @@ -2,11 +2,12 @@ import importlib import importlib.util import inspect import pkgutil +import types from abc import ABC, abstractmethod from collections import defaultdict from dataclasses import dataclass from functools import cache -from typing import Any, Iterable, TypeVar, Union +from typing import Any, Iterable, TypeVar, Union, get_args, get_origin import discord import regex @@ -121,13 +122,13 @@ def repr_equals(override: Any, default: Any) -> bool: def starting_value(type_: type[T]) -> T: """Return a value of the given type.""" - if hasattr(type_, "__origin__"): - if type_.__origin__ is not Union: # In case this is a types.GenericAlias or a typing._GenericAlias - type_ = type_.__origin__ - if hasattr(type_, "__args__"): # In case of a Union - if type(None) in type_.__args__: + if get_origin(type_) in (Union, types.UnionType): # In case of a Union + args = get_args(type_) + if type(None) in args: return None - type_ = type_.__args__[0] # Pick one, doesn't matter + type_ = args[0] # Pick one, doesn't matter + if origin := get_origin(type_): # In case of a parameterized List, Set, Dict etc. + type_ = origin try: return type_() -- cgit v1.2.3