Skip to content

Type Annotations for RestrictedPython#317

Draft
zedzhen wants to merge 46 commits into
zopefoundation:masterfrom
zedzhen:typing
Draft

Type Annotations for RestrictedPython#317
zedzhen wants to merge 46 commits into
zopefoundation:masterfrom
zedzhen:typing

Conversation

@zedzhen

@zedzhen zedzhen commented Jun 2, 2026

Copy link
Copy Markdown
Contributor
  • I signed and returned the Zope Contributor Agreement, and received and accepted an invitation to join a team in the zopefoundation GitHub organization.
  • I verified there aren't any other open pull requests for the same change.
  • I followed the guidelines in Developer guidelines.
  • I successfully ran code quality checks on my changes locally.
  • I successfully ran tests on my changes locally.
  • If needed, I added new tests for my changes.
  • If needed, I added documentation for my changes.
  • I included a change log entry in my commits.

This PR is based on #303. But PR #303 is not active.
Closes #303

loechel and others added 30 commits October 18, 2025 11:31
# Conflicts:
#	.meta.toml
#	CHANGES.rst
#	setup.py
#	pyproject.toml
#	src/RestrictedPython/transformer.py
# Conflicts:
#	.meta.toml

@icemac icemac left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of adding type annotations but I am missing checks that the type annotations are correct (mypy or ty preferably run via pre-commit.) Otherwise we cannot say that the current type annotations are correct and will stay correct with future changes.

Comment thread src/RestrictedPython/compile.py Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds first-class typing support (PEP 561) across RestrictedPython by introducing type annotations in key modules and marking the distribution as typed.

Changes:

  • Add/expand type annotations in the compiler and AST transformer code paths.
  • Introduce py.typed and advertise typing support via package metadata.
  • Minor typing-related cleanup in utilities and changelog messaging.

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
src/RestrictedPython/Utilities.py Adds typing for reorder() and imports Iterable.
src/RestrictedPython/transformer.py Adds extensive annotations to transformer helpers and visit_* methods.
src/RestrictedPython/py.typed Adds PEP 561 marker file for typed package distribution.
src/RestrictedPython/compile.py Adds annotations, CompileResult as NamedTuple, and typed signatures for compile helpers.
pyproject.toml Declares Typing :: Typed and adds a typecheck extra.
CHANGES.rst Adds changelog entry for type-annotation work.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/RestrictedPython/compile.py Outdated
Comment thread pyproject.toml
Comment thread src/RestrictedPython/transformer.py Outdated
Comment thread src/RestrictedPython/transformer.py Outdated
Comment thread src/RestrictedPython/compile.py Outdated
Comment thread src/RestrictedPython/compile.py Outdated
policy=RestrictingNodeTransformer):
source: str | ReadableBuffer | Module | Expression | Interactive,
filename: str | ReadableBuffer | PathLike[Any] = '<string>',
mode: Literal["exec", "eval", "single"] = "exec",
Comment thread src/RestrictedPython/compile.py Outdated
policy=RestrictingNodeTransformer):
source: str | ReadableBuffer | Module | Expression | Interactive,
filename: str | ReadableBuffer | PathLike[Any] = '<unknown>',
mode: str = 'exec',
@zedzhen zedzhen marked this pull request as draft June 3, 2026 07:29

@icemac icemac left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the changes. There are still some open conversations, could you please resolve them?

Comment thread src/RestrictedPython/transformer.py Outdated
_T_pos_ast: typing.TypeAlias = (
ast.stmt | ast.expr | ast.excepthandler | ast.arg | ast.keyword | ast.alias
| ast.pattern)
if sys.version_info >= (3, 12):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use _compat.IS_PY312_OR_GREATER instead

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mypy and other static type checkers have limited if/else support. "if sys.version_info >= (...)" they support, but "if some_bool_value" does not. I suggest allowing "if sys.version_info >= (...)" for types, but only in _types.py.

@zedzhen

zedzhen commented Jun 12, 2026

Copy link
Copy Markdown
Contributor Author

I like the changes. There are still some open conversations, could you please resolve them?

I will resolve them after #318 is resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants