diff --git a/tests/test_utils.py b/tests/test_utils.py index 3673155d6..0cbbacf42 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -38,9 +38,7 @@ def test_cached_properties(): # cached_slot_property class TestSlotted: - __slots__ = ( - '_cs_time' - ) + __slots__ = '_cs_time' @utils.cached_slot_property('_cs_time') def time(self) -> float: @@ -61,7 +59,7 @@ def test_cached_properties(): (123456789012345678, (2015, 12, 7, 16, 13, 12)), (661720302316814366, (2020, 1, 1, 0, 0, 14)), (1000000000000000000, (2022, 7, 22, 11, 22, 59)), - ] + ], ) def test_snowflake_time(snowflake: int, time_tuple: typing.Tuple[int, int, int, int, int, int]): dt = utils.snowflake_time(snowflake) @@ -74,10 +72,7 @@ def test_snowflake_time(snowflake: int, time_tuple: typing.Tuple[int, int, int, @pytest.mark.asyncio async def test_get_find(): # Generate a dictionary of random keys to values - mapping = { - secrets.token_bytes(32): secrets.token_bytes(32) - for _ in range(100) - } + mapping = {secrets.token_bytes(32): secrets.token_bytes(32) for _ in range(100)} # Turn it into a shuffled iterable of pairs pair = collections.namedtuple('pair', 'key value') @@ -136,7 +131,7 @@ def test_valid_icon_size(): ('https://discordapp.com/invite/dpy', 'dpy'), ('https://discord.com/invite/dpy', 'dpy'), ('https://discord.gg/dpy', 'dpy'), - ] + ], ) def test_resolve_invite(url, code): assert utils.resolve_invite(url).code == code @@ -148,16 +143,13 @@ def test_resolve_invite(url, code): ('https://discordapp.com/template/foobar', 'foobar'), ('https://discord.com/template/foobar', 'foobar'), ('https://discord.new/foobar', 'foobar'), - ] + ], ) def test_resolve_template(url, code): assert utils.resolve_template(url) == code -@pytest.mark.parametrize( - 'mention', - ['@everyone', '@here'] -) +@pytest.mark.parametrize('mention', ['@everyone', '@here']) def test_escape_mentions(mention): assert mention not in utils.escape_mentions(mention) assert mention not in utils.escape_mentions(f"one {mention} two") @@ -171,7 +163,7 @@ def test_escape_mentions(mention): ([1, 2, 3, 4, 5, 6], 3, [[1, 2, 3], [4, 5, 6]]), ([1, 2, 3, 4, 5, 6], 4, [[1, 2, 3, 4], [5, 6]]), ([1, 2, 3, 4, 5, 6], 5, [[1, 2, 3, 4, 5], [6]]), - ] + ], ) async def test_as_chunks(source, chunk_size, chunked): assert [x for x in utils.as_chunks(source, chunk_size)] == chunked @@ -185,12 +177,17 @@ async def test_as_chunks(source, chunk_size, chunked): ('datetime.datetime', datetime.datetime), ('typing.Union[typing.Literal["a"], typing.Literal["b"]]', typing.Union[typing.Literal["a"], typing.Literal["b"]]), ('typing.Union[typing.Union[int, str], typing.Union[bool, dict]]', typing.Union[int, str, bool, dict]), - ] + ], ) def test_resolve_annotation(annotation, resolved): assert resolved == utils.resolve_annotation(annotation, globals(), locals(), None) +def test_resolve_annotation_optional_normalisation(): + value = utils.resolve_annotation('typing.Union[None, int]', globals(), locals(), None) + assert value.__args__ == (int, type(None)) + + @pytest.mark.skipif(sys.version_info < (3, 10), reason="3.10 union syntax") @pytest.mark.parametrize( ('annotation', 'resolved'), @@ -198,7 +195,7 @@ def test_resolve_annotation(annotation, resolved): ('int | None', typing.Optional[int]), ('str | int', typing.Union[str, int]), ('str | int | None', typing.Optional[typing.Union[str, int]]), - ] + ], ) def test_resolve_annotation_310(annotation, resolved): assert resolved == utils.resolve_annotation(annotation, globals(), locals(), None) @@ -206,27 +203,33 @@ def test_resolve_annotation_310(annotation, resolved): # is_inside_class tests + def not_a_class(): def not_a_class_either(): pass + return not_a_class_either + class ThisIsAClass: def in_a_class(self): def not_directly_in_a_class(): pass + return not_directly_in_a_class @classmethod def a_class_method(cls): def not_directly_in_a_class(): pass + return not_directly_in_a_class @staticmethod def a_static_method(): def not_directly_in_a_class(): pass + return not_directly_in_a_class class SubClass: @@ -255,7 +258,7 @@ def test_is_inside_class(): (datetime.datetime(2020, 1, 1, 0, 0, 0, 0, tzinfo=datetime.timezone.utc), None, ''), (datetime.datetime(2020, 1, 1, 0, 0, 0, 0, tzinfo=datetime.timezone.utc), 'F', ''), (datetime.datetime(2033, 5, 18, 3, 33, 20, 0, tzinfo=datetime.timezone.utc), 'D', ''), - ] + ], ) def test_format_dt(dt: datetime.datetime, style: typing.Optional[utils.TimestampStyle], formatted: str): assert utils.format_dt(dt, style=style) == formatted