Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug with TypedDict for typing_extensions specifically #145

Merged
merged 1 commit into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install tox tox-gh-actions poetry==1.2.2
pip install 'tox<5' tox-gh-actions 'poetry<1.5'
- name: Test with tox
run: tox

Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

0.16.5 (2023-03-01)
-------------------

- Don't flag ``TypedDict`` from ``typing_extensions`` in Python versions
where ``typing`` has ``TypedDict`` itself.

0.16.4 (2023-01-13)
-------------------

Expand Down
2 changes: 1 addition & 1 deletion docs/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Use the following configuration:

repos:
- repo: https://github.com/ariebovenberg/slotscheck
rev: v0.16.4
rev: v0.16.5
hooks:
- id: slotscheck
# If your Python files are not importable from the project root,
Expand Down
257 changes: 72 additions & 185 deletions poetry.lock

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "slotscheck"
version = "0.16.4"
version = "0.16.5"
description = "Ensure your __slots__ are working properly."
authors = ["Arie Bovenberg <a.c.bovenberg@gmail.com>"]
license = "MIT"
Expand Down Expand Up @@ -34,12 +34,11 @@ flake8 = "^5.0.4"
isort = "^5.11.5"
mypy = "^1.0"
pytest = "^7.2.1"
tox = "^3.27.1"
black = "^23.1"
pytest-cov = "^4.0.0"
pytest-mock = "^3.6.1"
types-dataclasses = "^0.6.6"
# Not actually used, but a rare library whose __init__ is an extension.
typing_extensions = ">=4.1,<5"
# Only actually needed as an example library whose __init__ is an extension.
pydantic = "1.9.2"

[tool.poetry.scripts]
Expand Down
13 changes: 12 additions & 1 deletion src/slotscheck/checks.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
"Slots-related checks and inspection tools"
import platform
import sys
from typing import Collection, Iterator, Optional
from typing import Callable, Collection, Iterator, Optional

from .common import either

is_typeddict: Callable[[type], bool]

try:
from typing import is_typeddict

try:
from typing_extensions import is_typeddict as _is_typing_ext_typeddict
except ImportError: # pragma: no cover
pass
else:
is_typeddict = either(is_typeddict, _is_typing_ext_typeddict)
except ImportError: # pragma: no cover
from typing_extensions import is_typeddict

Expand Down
17 changes: 17 additions & 0 deletions src/slotscheck/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Set,
Tuple,
TypeVar,
overload,
)

flatten = chain.from_iterable
Expand Down Expand Up @@ -69,6 +70,22 @@ def both(__a: Predicate[_T1], __b: Predicate[_T1]) -> Predicate[_T1]:
return lambda x: __a(x) and __b(x)


@overload
def either(
__a: Callable[[_T1], bool], __b: Callable[[_T1], bool]
) -> Callable[[_T1], bool]:
...


@overload
def either(__a: Predicate[_T1], __b: Predicate[_T1]) -> Predicate[_T1]:
...


def either(__a: Any, __b: Any) -> Any:
return lambda x: __a(x) or __b(x)


def map_optional(
f: Callable[[_T1], Optional[_T2]], it: Iterable[_T1]
) -> Iterator[_T2]:
Expand Down
10 changes: 9 additions & 1 deletion tests/src/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
from xml.etree.ElementTree import Element

import pytest
from typing_extensions import TypedDict as TypingExtensionsTypedDict

from slotscheck.checks import has_slotless_base, has_slots, slots_overlap

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


class HasSlots:
Expand Down Expand Up @@ -73,6 +74,10 @@ class MyDict(TypedDict):
foo: str


class MyTypingExtensionsTypedDict(TypingExtensionsTypedDict):
bla: int


class TestHasSlots:
@pytest.mark.parametrize(
"klass",
Expand All @@ -84,6 +89,9 @@ def test_not_purepython(self, klass):
def test_typeddict(self):
assert has_slots(MyDict)

def test_typing_extensions_typeddict(self):
assert has_slots(MyTypingExtensionsTypedDict)

@pytest.mark.parametrize(
"klass",
[
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ fail_under=100
exclude_lines=
pragma: no cover
raise NotImplementedError
\.\.\.

[gh-actions]
python =
Expand Down