Skip to content

Commit

Permalink
gh-104050: Add more type annotations to Argument Clinic (#104628)
Browse files Browse the repository at this point in the history
Annotate the following:

- methods of class Class
- methods of class Module
- methods of class PythonParser
- function compute_checksum()
- function parse_file()
- global variable unsupported_special_methods
  • Loading branch information
erlend-aasland authored May 18, 2023
1 parent 86ee49f commit 94c8eda
Showing 1 changed file with 39 additions and 14 deletions.
53 changes: 39 additions & 14 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2229,7 +2229,12 @@ def _module_and_class(self, fields):
return module, cls


def parse_file(filename, *, verify=True, output=None):
def parse_file(
filename: str,
*,
verify: bool = True,
output: str | None = None
) -> None:
if not output:
output = filename

Expand Down Expand Up @@ -2261,7 +2266,10 @@ def parse_file(filename, *, verify=True, output=None):
write_file(fn, data)


def compute_checksum(input, length=None):
def compute_checksum(
input: str | None,
length: int | None = None
) -> str:
input = input or ''
s = hashlib.sha1(input.encode('utf-8')).hexdigest()
if length:
Expand All @@ -2272,44 +2280,61 @@ def compute_checksum(input, length=None):


class PythonParser:
def __init__(self, clinic):
def __init__(self, clinic: Clinic) -> None:
pass

def parse(self, block):
def parse(self, block: Block) -> None:
s = io.StringIO()
with OverrideStdioWith(s):
exec(block.input)
block.output = s.getvalue()


ModuleDict = dict[str, "Module"]

class Module:
def __init__(self, name, module=None):
def __init__(
self,
name: str,
module = None
) -> None:
self.name = name
self.module = self.parent = module

self.modules = collections.OrderedDict()
self.classes = collections.OrderedDict()
self.functions = []
self.modules: ModuleDict = collections.OrderedDict()
self.classes: ClassDict = collections.OrderedDict()
self.functions: list[Function] = []

def __repr__(self):
def __repr__(self) -> str:
return "<clinic.Module " + repr(self.name) + " at " + str(id(self)) + ">"


ClassDict = dict[str, "Class"]

class Class:
def __init__(self, name, module=None, cls=None, typedef=None, type_object=None):
def __init__(
self,
name: str,
module: Module | None = None,
cls = None,
typedef: str | None = None,
type_object: str | None = None
) -> None:
self.name = name
self.module = module
self.cls = cls
self.typedef = typedef
self.type_object = type_object
self.parent = cls or module

self.classes = collections.OrderedDict()
self.functions = []
self.classes: ClassDict = collections.OrderedDict()
self.functions: list[Function] = []

def __repr__(self):
def __repr__(self) -> str:
return "<clinic.Class " + repr(self.name) + " at " + str(id(self)) + ">"

unsupported_special_methods = set("""

unsupported_special_methods: set[str] = set("""
__abs__
__add__
Expand Down

0 comments on commit 94c8eda

Please sign in to comment.