Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Slightly annotate clinic.py
  • Loading branch information
erlend-aasland committed May 1, 2023
commit d2ec2ef36d0a387aa13429ddf493c3538ada7ef6
30 changes: 18 additions & 12 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import types

from types import *
Comment thread
hugovk marked this conversation as resolved.
from typing import TypeVar, Callable

# TODO:
#
Expand Down Expand Up @@ -68,7 +69,7 @@ def __repr__(self):
sig_end_marker = '--'


_text_accumulator_nt = collections.namedtuple("_text_accumulator", "text append output")
_text_accumulator_nt = collections.namedtuple("_text_accumulator_nt", "text append output")

def _text_accumulator():
text = []
Expand All @@ -79,7 +80,7 @@ def output():
return _text_accumulator_nt(text, text.append, output)


text_accumulator_nt = collections.namedtuple("text_accumulator", "text append")
text_accumulator_nt = collections.namedtuple("text_accumulator_nt", "text append")

def text_accumulator():
"""
Expand Down Expand Up @@ -1915,8 +1916,11 @@ def dump(self):
# maps strings to Language objects.
# "languages" maps the name of the language ("C", "Python").
# "extensions" maps the file extension ("c", "py").
L = TypeVar('L', bound=Language)
LangDict = dict[str, L]

languages = { 'C': CLanguage, 'Python': PythonLanguage }
extensions = { name: CLanguage for name in "c cc cpp cxx h hh hpp hxx".split() }
extensions: LangDict = { name: CLanguage for name in "c cc cpp cxx h hh hpp hxx".split() }
extensions['py'] = PythonLanguage


Expand Down Expand Up @@ -2550,6 +2554,8 @@ def __init__(cls, name, bases, classdict):
add_c_converter(cls)
add_default_legacy_c_converter(cls)

from typing import Type

class CConverter(metaclass=CConverterAutoRegister):
"""
For the init function, self, name, function, and default
Expand All @@ -2558,15 +2564,15 @@ class CConverter(metaclass=CConverterAutoRegister):
"""

# The C name to use for this variable.
name = None
name: str | None = None
Copy link
Copy Markdown
Contributor

@erlend-aasland erlend-aasland May 16, 2023

Choose a reason for hiding this comment

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

So, these str | None are now starting to cause frustrations, as I gradually add typing to other parts of clinic.py. Since there are no guards anywhere, I obviously get errors and warnings about str plus None, etc. We could change the defaults to the empty string, but that can also create a lot of havoc.


# The Python name to use for this variable.
py_name = None
py_name: str | None = None

# The C type to use for this variable.
# 'type' should be a Python string specifying the type, e.g. "int".
# If this is a pointer type, the type string should end with ' *'.
type = None
type: str | None = None

# The Python default value for this parameter, as a Python value.
# Or the magic value "unspecified" if there is no default.
Expand All @@ -2577,15 +2583,15 @@ class CConverter(metaclass=CConverterAutoRegister):

# If not None, default must be isinstance() of this type.
# (You can also specify a tuple of types.)
default_type = None
default_type: Type | tuple[Type, ...] | None = None

# "default" converted into a C value, as a string.
# Or None if there is no default.
c_default = None
c_default: str | None = None

# "default" converted into a Python value, as a string.
# Or None if there is no default.
py_default = None
py_default: str | None = None

# The default value used to initialize the C variable when
# there is no default, but not specifying a default may
Expand All @@ -2597,11 +2603,11 @@ class CConverter(metaclass=CConverterAutoRegister):
#
# This value is specified as a string.
# Every non-abstract subclass should supply a valid value.
c_ignored_default = 'NULL'
c_ignored_default: str = 'NULL'

# The C converter *function* to be used, if any.
# (If this is not None, format_unit must be 'O&'.)
converter = None
converter: str | None = None

# Should Argument Clinic add a '&' before the name of
# the variable when passing it into the _impl function?
Expand Down Expand Up @@ -3414,7 +3420,7 @@ class robuffer: pass
def str_converter_key(types, encoding, zeroes):
return (frozenset(types), bool(encoding), bool(zeroes))

str_converter_argument_map = {}
str_converter_argument_map: dict[str, str] = {}

class str_converter(CConverter):
type = 'const char *'
Expand Down