From 90582483bb238dd537b4a2cff1f717d283bd6b73 Mon Sep 17 00:00:00 2001 From: Arie Bovenberg Date: Mon, 21 Nov 2022 21:18:30 +0100 Subject: [PATCH] fix false positive for typeddicts --- CHANGELOG.rst | 5 +++++ docs/advanced.rst | 2 +- pyproject.toml | 2 +- src/slotscheck/checks.py | 8 +++++--- tests/src/test_checks.py | 8 ++++++++ 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 3b135e7..11ddd0b 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,6 +1,11 @@ Changelog ========= +0.16.1 (2022-11-21) +------------------- + +- Don't flag ``TypedDict`` subclasses as missing slots (#120). + 0.16.0 (2022-11-01) ------------------- diff --git a/docs/advanced.rst b/docs/advanced.rst index a57cbf4..1e71279 100644 --- a/docs/advanced.rst +++ b/docs/advanced.rst @@ -18,7 +18,7 @@ Use the following configuration: repos: - repo: https://github.com/ariebovenberg/slotscheck - rev: v0.16.0 + rev: v0.16.1 hooks: - id: slotscheck # If your Python files are not importable from the project root, diff --git a/pyproject.toml b/pyproject.toml index 80277d1..b02cac8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "slotscheck" -version = "0.16.0" +version = "0.16.1" description = "Ensure your __slots__ are working properly." authors = ["Arie Bovenberg "] license = "MIT" diff --git a/src/slotscheck/checks.py b/src/slotscheck/checks.py index 84e1ab7..7dda820 100644 --- a/src/slotscheck/checks.py +++ b/src/slotscheck/checks.py @@ -1,7 +1,7 @@ "Slots-related checks and inspection tools" import platform import sys -from typing import Collection, Iterator, Optional +from typing import Collection, Iterator, Optional, is_typeddict def slots(c: type) -> Optional[Collection[str]]: @@ -18,8 +18,10 @@ def slots(c: type) -> Optional[Collection[str]]: def has_slots(c: type) -> bool: - return "__slots__" in c.__dict__ or not ( - issubclass(c, BaseException) or is_pure_python(c) + return ( + "__slots__" in c.__dict__ + or not (issubclass(c, BaseException) or is_pure_python(c)) + or is_typeddict(c) ) diff --git a/tests/src/test_checks.py b/tests/src/test_checks.py index cad3773..64fff44 100644 --- a/tests/src/test_checks.py +++ b/tests/src/test_checks.py @@ -4,6 +4,7 @@ from enum import Enum from fractions import Fraction from random import Random +from typing import TypedDict from xml.etree.ElementTree import Element import pytest @@ -64,6 +65,10 @@ class Foo(metaclass=FooMeta): __slots__ = () +class MyDict(TypedDict): + foo: str + + class TestHasSlots: @pytest.mark.parametrize( "klass", @@ -72,6 +77,9 @@ class TestHasSlots: def test_not_purepython(self, klass): assert has_slots(klass) + def test_typeddict(self): + assert has_slots(MyDict) + @pytest.mark.parametrize( "klass", [