Skip to content

Commit 75a7796

Browse files
committed
Add typing annotations
Initial draft courtesy of `monkeytype`: ```shell .venv/bin/uv pip install -e plone.api[test] monkeytype zope.testrunner monkeytype run .venv/bin/zope-testrunner --path plone.api/src/ for module in `monkeytype list-modules`; do monkeytype apply $module || true > /dev/null;done ```
1 parent 0526b7c commit 75a7796

19 files changed

Lines changed: 382 additions & 148 deletions

.meta.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ commit-id = "2.5.1"
99
codespell_extra_lines = """
1010
exclude: docs/locale/.*.pot
1111
"""
12+
extra_lines = """
13+
- repo: https://github.com/pre-commit/mirrors-mypy
14+
rev: v1.18.2
15+
hooks:
16+
- id: mypy
17+
additional_dependencies:
18+
- types-decorator
19+
exclude: docs
20+
"""
1221

1322
[pyproject]
1423
check_manifest_ignores = """

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ repos:
7676
hooks:
7777
- id: i18ndude
7878

79+
- repo: https://github.com/pre-commit/mirrors-mypy
80+
rev: v1.18.2
81+
hooks:
82+
- id: mypy
83+
additional_dependencies:
84+
- types-decorator
85+
exclude: docs
7986

8087
##
8188
# Add extra configuration options in .meta.toml:

news/+a04874c0.internal.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add typing annotations [@ale-rt]

news/+ec620103.internal.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
More typing annotations for the typing module

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"plone.dexterity",
3737
"plone.i18n",
3838
"plone.registry",
39+
"plone.supermodel",
3940
"plone.uuid",
4041
"zope.globalrequest",
4142
"Products.CMFCore",

src/plone/api/_types.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""Shared internal typing aliases used across plone.api modules."""
2+
3+
from Acquisition import ImplicitAcquisitionWrapper
4+
from typing import TypeAlias
5+
from ZPublisher.HTTPRequest import HTTPRequest
6+
7+
try:
8+
from plone.dexterity.content import DexterityContent as _DexterityContext
9+
except ImportError: # pragma: no cover
10+
_DexterityContext = ImplicitAcquisitionWrapper
11+
12+
13+
# Public-facing context type for content APIs.
14+
Context: TypeAlias = _DexterityContext
15+
16+
# Broader acquisition-wrapped context for APIs that can accept portal/tool roots.
17+
ContainerContext: TypeAlias = ImplicitAcquisitionWrapper
18+
19+
# Shared request type for browser/request-aware APIs.
20+
Request: TypeAlias = HTTPRequest

src/plone/api/addon.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from plone.base.utils import get_installer
1212
from Products.CMFPlone.controlpanel.browser.quickinstaller import InstallerView
1313
from Products.GenericSetup import EXTENSION
14+
from typing import Any
15+
from typing import cast
1416
from zope.component import getAllUtilitiesRegisteredFor
1517
from zope.globalrequest import getRequest
1618

@@ -94,7 +96,7 @@ def _get_non_installable_addons() -> NonInstallableAddons:
9496

9597

9698
@lru_cache(maxsize=1)
97-
def _cached_addons() -> tuple[tuple[str, AddonInformation]]:
99+
def _cached_addons() -> tuple[tuple[str, AddonInformation], ...]:
98100
"""Return information about add-ons in this installation.
99101
100102
:returns: Tuple of tuples with add-on id and AddonInformation.
@@ -130,7 +132,7 @@ def _cached_addons() -> tuple[tuple[str, AddonInformation]]:
130132
profile_type = pid_parts[-1]
131133
if product_id not in addons:
132134
# get some basic information on the product
133-
product = {
135+
product: dict[str, Any] = {
134136
"id": product_id,
135137
"version": get_version(product_id),
136138
"title": product_id,
@@ -274,7 +276,9 @@ def get(addon: str) -> AddonInformation:
274276
addons = dict(_cached_addons())
275277
if addon not in addons:
276278
raise InvalidParameterError(f"No add-on {addon} found.")
277-
return _update_addon_info(addons.get(addon), _get_installer())
279+
return _update_addon_info(
280+
cast(AddonInformation, addons.get(addon)), _get_installer()
281+
)
278282

279283

280284
@required_parameters("addon")

0 commit comments

Comments
 (0)