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 708335d commit 9058248
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 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
2 changes: 1 addition & 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 Down
8 changes: 5 additions & 3 deletions 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]]:
Expand All @@ -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)
)


Expand Down
8 changes: 8 additions & 0 deletions tests/src/test_checks.py
Expand Up @@ -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
Expand Down Expand Up @@ -64,6 +65,10 @@ class Foo(metaclass=FooMeta):
__slots__ = ()


class MyDict(TypedDict):
foo: str


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

0 comments on commit 9058248

Please sign in to comment.