Skip to content

Commit

Permalink
Merge pull request #152 from pallets-eco/pyright-export
Browse files Browse the repository at this point in the history
pass `pyright --verifytypes`
  • Loading branch information
davidism authored Apr 28, 2024
2 parents f1bbce1 + cceb745 commit d15107c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Unreleased
- Restore identity handling for ``str`` and ``int`` senders. :pr:`148`
- Fix deprecated ``blinker.base.WeakNamespace`` import. :pr:`149`
- Use types from ``collections.abc`` instead of ``typing``. :pr:`150`
- Fully specify exported types as reported by pyright. :pr:`152`


Version 1.8.0
Expand Down
35 changes: 22 additions & 13 deletions src/blinker/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,18 @@ def __repr__(self) -> str:
return f"{base[:-1]}; {self.name!r}>" # noqa: E702


class Namespace(dict): # type: ignore[type-arg]
if t.TYPE_CHECKING:

class PNamespaceSignal(t.Protocol):
def __call__(self, name: str, doc: str | None = None) -> NamedSignal: ...

# Python < 3.9
_NamespaceBase = dict[str, NamedSignal] # type: ignore[misc]
else:
_NamespaceBase = dict


class Namespace(_NamespaceBase):
"""A dict mapping names to signals."""

def signal(self, name: str, doc: str | None = None) -> NamedSignal:
Expand All @@ -539,11 +550,10 @@ def signal(self, name: str, doc: str | None = None) -> NamedSignal:
:param name: The name of the signal.
:param doc: The docstring of the signal.
"""
try:
return self[name] # type: ignore[no-any-return]
except KeyError:
result = self.setdefault(name, NamedSignal(name, doc))
return result # type: ignore[no-any-return]
if name not in self:
self[name] = NamedSignal(name, doc)

return self[name]


class _WeakNamespace(WeakValueDictionary): # type: ignore[type-arg]
Expand Down Expand Up @@ -575,19 +585,18 @@ def signal(self, name: str, doc: str | None = None) -> NamedSignal:
:param name: The name of the signal.
:param doc: The docstring of the signal.
"""
try:
return self[name] # type: ignore[no-any-return]
except KeyError:
result = self.setdefault(name, NamedSignal(name, doc))
return result # type: ignore[no-any-return]
if name not in self:
self[name] = NamedSignal(name, doc)

return self[name] # type: ignore[no-any-return]


default_namespace = Namespace()
default_namespace: Namespace = Namespace()
"""A default :class:`Namespace` for creating named signals. :func:`signal`
creates a :class:`NamedSignal` in this namespace.
"""

signal = default_namespace.signal
signal: PNamespaceSignal = default_namespace.signal
"""Return a :class:`NamedSignal` in :data:`default_namespace` with the given
``name``, creating it if required. Repeated calls with the same name return the
same signal.
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ deps = -r requirements/typing.txt
commands =
mypy
pyright
pyright --verifytypes blinker --ignoreexternal

[testenv:docs]
deps = -r requirements/docs.txt
Expand Down

0 comments on commit d15107c

Please sign in to comment.