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

gh-104050: Add more type annotations to Argument Clinic #104631

Merged
merged 8 commits into from
May 21, 2023
Merged
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
67 changes: 44 additions & 23 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2395,6 +2395,9 @@ def __repr__(self) -> str:
INVALID, CALLABLE, STATIC_METHOD, CLASS_METHOD, METHOD_INIT, METHOD_NEW
""".replace(",", "").strip().split()

ParamDict = dict[str, "Parameter"]
ReturnConverterType = Callable[..., "CReturnConverter"]

class Function:
"""
Mutable duck type for inspect.Function.
Expand All @@ -2407,12 +2410,22 @@ class Function:
(not docstring) or ((not docstring[0].isspace()) and (docstring.rstrip() == docstring))
"""

def __init__(self, parameters=None, *, name,
module, cls=None, c_basename=None,
full_name=None,
return_converter, return_annotation=inspect.Signature.empty,
docstring=None, kind=CALLABLE, coexist=False,
docstring_only=False):
def __init__(
self,
parameters: ParamDict | None = None,
*,
name: str,
module: Module,
cls: Class | None = None,
c_basename: str | None = None,
full_name: str | None = None,
return_converter: ReturnConverterType,
return_annotation = inspect.Signature.empty,
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved
docstring: str | None = None,
kind: str = CALLABLE,
coexist: bool = False,
docstring_only: bool = False
) -> None:
self.parameters = parameters or {}
self.return_annotation = return_annotation
self.name = name
Expand Down Expand Up @@ -2446,7 +2459,7 @@ def render_parameters(self):
return self.__render_parameters__

@property
def methoddef_flags(self):
def methoddef_flags(self) -> str | None:
if self.kind in (METHOD_INIT, METHOD_NEW):
return None
flags = []
Expand All @@ -2460,10 +2473,10 @@ def methoddef_flags(self):
flags.append('METH_COEXIST')
return '|'.join(flags)

def __repr__(self):
def __repr__(self) -> str:
return '<clinic.Function ' + self.name + '>'

def copy(self, **overrides):
def copy(self, **overrides) -> "Function":
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved
kwargs = {
'name': self.name, 'module': self.module, 'parameters': self.parameters,
'cls': self.cls, 'c_basename': self.c_basename,
Expand All @@ -2486,9 +2499,18 @@ class Parameter:
Mutable duck type of inspect.Parameter.
"""

def __init__(self, name, kind, *, default=inspect.Parameter.empty,
function, converter, annotation=inspect.Parameter.empty,
docstring=None, group=0):
def __init__(
self,
name: str,
kind: str,
*,
default = inspect.Parameter.empty,
function: Function,
converter: "CConverter",
annotation = inspect.Parameter.empty,
docstring: str | None = None,
group: int = 0
) -> None:
self.name = name
self.kind = kind
self.default = default
Expand All @@ -2498,22 +2520,22 @@ def __init__(self, name, kind, *, default=inspect.Parameter.empty,
self.docstring = docstring or ''
self.group = group

def __repr__(self):
def __repr__(self) -> str:
return '<clinic.Parameter ' + self.name + '>'

def is_keyword_only(self):
def is_keyword_only(self) -> bool:
return self.kind == inspect.Parameter.KEYWORD_ONLY

def is_positional_only(self):
def is_positional_only(self) -> bool:
return self.kind == inspect.Parameter.POSITIONAL_ONLY

def is_vararg(self):
def is_vararg(self) -> bool:
return self.kind == inspect.Parameter.VAR_POSITIONAL

def is_optional(self):
def is_optional(self) -> bool:
return not self.is_vararg() and (self.default is not unspecified)

def copy(self, **overrides):
def copy(self, **overrides) -> "Parameter":
kwargs = {
'name': self.name, 'kind': self.kind, 'default':self.default,
'function': self.function, 'converter': self.converter, 'annotation': self.annotation,
Expand All @@ -2526,7 +2548,7 @@ def copy(self, **overrides):
kwargs['converter'] = converter
return Parameter(**kwargs)

def get_displayname(self, i):
def get_displayname(self, i: int) -> str:
if i == 0:
return '"argument"'
if not self.is_positional_only():
Expand All @@ -2537,13 +2559,13 @@ def get_displayname(self, i):

class LandMine:
# try to access any
def __init__(self, message):
def __init__(self, message: str) -> None:
self.__message__ = message

def __repr__(self):
def __repr__(self) -> str:
return '<LandMine ' + repr(self.__message__) + ">"

def __getattribute__(self, name):
def __getattribute__(self, name: str):
if name in ('__repr__', '__message__'):
return super().__getattribute__(name)
# raise RuntimeError(repr(name))
Expand Down Expand Up @@ -3002,7 +3024,6 @@ def parser_name(self):
# The callable may have any number of keyword-only parameters.
# The callable must return a CReturnConverter object.
# The callable should not call builtins.print.
ReturnConverterType = Callable[..., "CReturnConverter"]
ReturnConverterDict = dict[str, ReturnConverterType]
return_converters: ReturnConverterDict = {}

Expand Down