Skip to content

Commit

Permalink
fix false positive for typeddicts
Browse files Browse the repository at this point in the history
  • Loading branch information
ariebovenberg committed Nov 21, 2022
1 parent 4ffe482 commit 3cbcdd8
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 17 deletions.
5 changes: 5 additions & 0 deletions 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)
-------------------

Expand Down
2 changes: 1 addition & 1 deletion docs/advanced.rst
Expand Up @@ -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,
Expand Down
26 changes: 13 additions & 13 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion 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 <a.c.bovenberg@gmail.com>"]
license = "MIT"
Expand All @@ -25,6 +25,7 @@ homepage = "https://github.com/ariebovenberg/slotscheck"
[tool.poetry.dependencies]
python = ">=3.7,<4"
importlib-metadata = {version = ">=1,<5", python = "<3.8"}
typing-extensions = {version = ">=4.1,<5", python = "<3.10"}
dataclasses = {version = ">=0.6", python = "<3.7"}
click = "^8.0"
tomli = ">=0.2.6,<3.0.0"
Expand Down
11 changes: 9 additions & 2 deletions src/slotscheck/checks.py
Expand Up @@ -3,6 +3,11 @@
import sys
from typing import Collection, Iterator, Optional

try:
from typing import is_typeddict
except ImportError: # pragma: no cover
from typing_extensions import is_typeddict


def slots(c: type) -> Optional[Collection[str]]:
try:
Expand All @@ -18,8 +23,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)
)


Expand Down
12 changes: 12 additions & 0 deletions tests/src/test_checks.py
Expand Up @@ -10,6 +10,11 @@

from slotscheck.checks import has_slotless_base, has_slots, slots_overlap

try:
from typing import TypedDict
except ImportError:
from typing_extensions import TypedDict


class HasSlots:
__slots__ = ("a", "b")
Expand Down Expand Up @@ -64,6 +69,10 @@ class Foo(metaclass=FooMeta):
__slots__ = ()


class MyDict(TypedDict):
foo: str


class TestHasSlots:
@pytest.mark.parametrize(
"klass",
Expand All @@ -72,6 +81,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",
[
Expand Down

0 comments on commit 3cbcdd8

Please sign in to comment.