-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
gh-104050: Add basic typing to CConverter in clinic.py #104538
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
Merged
erlend-aasland
merged 2 commits into
python:main
from
erlend-aasland:typed-clinic/cconverter1
May 16, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2669,15 +2669,15 @@ class CConverter(metaclass=CConverterAutoRegister): | |
# keep in sync with self_converter.__init__! | ||
def __init__(self, | ||
# Positional args: | ||
name, | ||
py_name, | ||
name: str, | ||
py_name: str, | ||
function, | ||
default=unspecified, | ||
*, # Keyword only args: | ||
c_default=None, | ||
py_default=None, | ||
annotation=unspecified, | ||
unused=False, | ||
c_default: str | None = None, | ||
py_default: str | None = None, | ||
annotation: str | Unspecified = unspecified, | ||
unused: bool = False, | ||
**kwargs | ||
): | ||
self.name = ensure_legal_c_identifier(name) | ||
|
@@ -2713,10 +2713,10 @@ def __init__(self, | |
def converter_init(self): | ||
pass | ||
|
||
def is_optional(self): | ||
def is_optional(self) -> bool: | ||
return (self.default is not unspecified) | ||
|
||
def _render_self(self, parameter, data): | ||
def _render_self(self, parameter: str, data: CRenderData) -> None: | ||
self.parameter = parameter | ||
name = self.parser_name | ||
|
||
|
@@ -2776,7 +2776,7 @@ def _render_non_self(self, parameter, data): | |
if cleanup: | ||
data.cleanup.append('/* Cleanup for ' + name + ' */\n' + cleanup.rstrip() + "\n") | ||
|
||
def render(self, parameter, data): | ||
def render(self, parameter: str, data: CRenderData) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks fishy. |
||
""" | ||
parameter is a clinic.Parameter instance. | ||
data is a CRenderData instance. | ||
|
@@ -2852,31 +2852,31 @@ def declaration(self, *, in_parser=False): | |
declaration.append(';') | ||
return "".join(declaration) | ||
|
||
def initialize(self): | ||
def initialize(self) -> str: | ||
""" | ||
The C statements required to set up this variable before parsing. | ||
Returns a string containing this code indented at column 0. | ||
If no initialization is necessary, returns an empty string. | ||
""" | ||
return "" | ||
|
||
def modify(self): | ||
def modify(self) -> str: | ||
""" | ||
The C statements required to modify this variable after parsing. | ||
Returns a string containing this code indented at column 0. | ||
If no modification is necessary, returns an empty string. | ||
""" | ||
return "" | ||
|
||
def post_parsing(self): | ||
def post_parsing(self) -> str: | ||
""" | ||
The C statements required to do some operations after the end of parsing but before cleaning up. | ||
Return a string containing this code indented at column 0. | ||
If no operation is necessary, return an empty string. | ||
""" | ||
return "" | ||
|
||
def cleanup(self): | ||
def cleanup(self) -> str: | ||
""" | ||
The C statements required to clean up after this variable. | ||
Returns a string containing this code indented at column 0. | ||
|
@@ -2929,7 +2929,7 @@ def parse_arg(self, argname, displayname): | |
""".format(argname=argname, paramname=self.parser_name, cast=cast) | ||
return None | ||
|
||
def set_template_dict(self, template_dict): | ||
def set_template_dict(self, template_dict: dict[str, str]): | ||
pass | ||
|
||
@property | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't need to be done now (and you may not like the suggestion), but you could use an enum for all the sentinels that are defined in
clinic.py
, instead of defining three separate classes (Unspecified
,Null
andUnknown
):If you did that, then you'd be able to use a
typing.Literal
annotation here, which would be more expressive of the fact thatunspecified
is a singleton:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat!