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

pass pyright --verifytypes #152

Merged
merged 1 commit into from
Apr 28, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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