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

feat(typing): annotate routing package #2327

Merged
merged 17 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
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
90 changes: 43 additions & 47 deletions falcon/routing/compiled.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@
else:
return None

def insert(nodes: List[CompiledRouterNode], path_index: int = 0):
def insert(nodes: List[CompiledRouterNode], path_index: int = 0) -> None:
for node in nodes:
segment = path[path_index]
if node.matches(segment):
Expand Down Expand Up @@ -351,12 +351,7 @@
# issue.
if not iscoroutinefunction(responder) and is_python_func(responder):
if _should_wrap_non_coroutines():

def let(responder=responder):
method_map[method] = wrap_sync_to_async(responder)

let()

method_map[method] = wrap_sync_to_async(responder)

Check warning on line 354 in falcon/routing/compiled.py

View check run for this annotation

Codecov / codecov/patch

falcon/routing/compiled.py#L354

Added line #L354 was not covered by tests
else:
msg = (
'The {} responder must be a non-blocking '
Expand Down Expand Up @@ -515,12 +510,13 @@

else:
# NOTE(kgriffs): Simple nodes just capture the entire path
# segment as the value for the param.
# segment as the value for the param. They have a var_name defined

field_name = node.var_name
assert field_name is not None

Check warning on line 516 in falcon/routing/compiled.py

View check run for this annotation

Codecov / codecov/patch

falcon/routing/compiled.py#L515-L516

Added lines #L515 - L516 were not covered by tests
if node.var_converter_map:
assert len(node.var_converter_map) == 1

field_name = node.var_name
__, converter_name, converter_argstr = node.var_converter_map[0]
converter_class = self._converter_map[converter_name]

Expand All @@ -547,7 +543,7 @@
parent.append_child(cx_converter)
parent = cx_converter
else:
params_stack.append(_CxSetParamFromPath(node.var_name, level))
params_stack.append(_CxSetParamFromPath(field_name, level))

Check warning on line 546 in falcon/routing/compiled.py

View check run for this annotation

Codecov / codecov/patch

falcon/routing/compiled.py#L546

Added line #L546 was not covered by tests

# NOTE(kgriffs): We don't allow multiple simple var nodes
# to exist at the same level, e.g.:
Expand Down Expand Up @@ -745,7 +741,7 @@
method_map: Optional[MethodDict] = None,
resource: Optional[object] = None,
uri_template: Optional[str] = None,
):
) -> None:
self.children: List[CompiledRouterNode] = []

self.raw_segment = raw_segment
Expand Down Expand Up @@ -833,12 +829,12 @@
if self.is_complex:
assert self.is_var

def matches(self, segment: str):
def matches(self, segment: str) -> bool:
"""Return True if this node matches the supplied template segment."""

return segment == self.raw_segment

def conflicts_with(self, segment: str):
def conflicts_with(self, segment: str) -> bool:
"""Return True if this node conflicts with a given template segment."""

# NOTE(kgriffs): This method assumes that the caller has already
Expand Down Expand Up @@ -900,11 +896,11 @@

data: Dict[str, Type[converters.BaseConverter]]

def __setitem__(self, name, converter):
def __setitem__(self, name: str, converter: Type[converters.BaseConverter]) -> None:
self._validate(name)
UserDict.__setitem__(self, name, converter)

def _validate(self, name):
def _validate(self, name: str) -> None:
if not _IDENTIFIER_PATTERN.match(name):
raise ValueError(
'Invalid converter name. Names may not be blank, and may '
Expand Down Expand Up @@ -948,14 +944,14 @@

__slots__ = ('converters',)

def __init__(self):
def __init__(self) -> None:
object.__setattr__(
self,
'converters',
ConverterDict((name, converter) for name, converter in converters.BUILTIN),
)

def __setattr__(self, name, value) -> None:
def __setattr__(self, name: str, value: Any) -> None:
if name == 'converters':
raise AttributeError('Cannot set "converters", please update it in place.')
super().__setattr__(name, value)
Expand All @@ -978,13 +974,13 @@
def __init__(self) -> None:
self._children: List[_CxElement] = []

def append_child(self, construct: _CxElement):
def append_child(self, construct: _CxElement) -> None:
self._children.append(construct)

def src(self, indentation: int) -> str:
return self._children_src(indentation + 1)

def _children_src(self, indentation):
def _children_src(self, indentation: int) -> str:
src_lines = [child.src(indentation) for child in self._children]

return '\n'.join(src_lines)
Expand All @@ -997,12 +993,12 @@


class _CxIfPathLength(_CxParent):
def __init__(self, comparison, length):
def __init__(self, comparison: str, length: int) -> None:
super().__init__()
self._comparison = comparison
self._length = length

def src(self, indentation):
def src(self, indentation: int) -> str:
template = '{0}if path_len {1} {2}:\n{3}'
return template.format(
_TAB_STR * indentation,
Expand All @@ -1013,12 +1009,12 @@


class _CxIfPathSegmentLiteral(_CxParent):
def __init__(self, segment_idx, literal):
def __init__(self, segment_idx: int, literal: str) -> None:
super().__init__()
self._segment_idx = segment_idx
self._literal = literal

def src(self, indentation):
def src(self, indentation: int) -> str:
template = "{0}if path[{1}] == '{2}':\n{3}"
return template.format(
_TAB_STR * indentation,
Expand All @@ -1029,13 +1025,13 @@


class _CxIfPathSegmentPattern(_CxParent):
def __init__(self, segment_idx, pattern_idx, pattern_text):
def __init__(self, segment_idx: int, pattern_idx: int, pattern_text: str) -> None:
super().__init__()
self._segment_idx = segment_idx
self._pattern_idx = pattern_idx
self._pattern_text = pattern_text

def src(self, indentation):
def src(self, indentation: int) -> str:
lines = [
'{0}match = patterns[{1}].match(path[{2}]) # {3}'.format(
_TAB_STR * indentation,
Expand All @@ -1051,13 +1047,13 @@


class _CxIfConverterField(_CxParent):
def __init__(self, unique_idx, converter_idx):
def __init__(self, unique_idx: int, converter_idx: int) -> None:
super().__init__()
self._converter_idx = converter_idx
self._unique_idx = unique_idx
self.field_variable_name = 'field_value_{0}'.format(unique_idx)

def src(self, indentation):
def src(self, indentation: int) -> str:
lines = [
'{0}{1} = converters[{2}].convert(fragment)'.format(
_TAB_STR * indentation,
Expand All @@ -1074,84 +1070,84 @@


class _CxSetFragmentFromField(_CxChild):
def __init__(self, field_name):
def __init__(self, field_name: str) -> None:
self._field_name = field_name

def src(self, indentation):
def src(self, indentation: int) -> str:
return "{0}fragment = groups.pop('{1}')".format(
_TAB_STR * indentation,
self._field_name,
)


class _CxSetFragmentFromPath(_CxChild):
def __init__(self, segment_idx):
def __init__(self, segment_idx: int) -> None:
self._segment_idx = segment_idx

def src(self, indentation):
def src(self, indentation: int) -> str:
return '{0}fragment = path[{1}]'.format(
_TAB_STR * indentation,
self._segment_idx,
)


class _CxSetFragmentFromRemainingPaths(_CxChild):
def __init__(self, segment_idx):
def __init__(self, segment_idx: int) -> None:
self._segment_idx = segment_idx

def src(self, indentation):
def src(self, indentation: int) -> str:
return '{0}fragment = path[{1}:]'.format(
_TAB_STR * indentation,
self._segment_idx,
)


class _CxVariableFromPatternMatch(_CxChild):
def __init__(self, unique_idx):
def __init__(self, unique_idx: int) -> None:
self._unique_idx = unique_idx
self.dict_variable_name = 'dict_match_{0}'.format(unique_idx)

def src(self, indentation):
def src(self, indentation: int) -> str:
return '{0}{1} = match.groupdict()'.format(
_TAB_STR * indentation, self.dict_variable_name
)


class _CxVariableFromPatternMatchPrefetched(_CxChild):
def __init__(self, unique_idx):
def __init__(self, unique_idx: int) -> None:
self._unique_idx = unique_idx
self.dict_variable_name = 'dict_groups_{0}'.format(unique_idx)

def src(self, indentation):
def src(self, indentation: int) -> str:
return '{0}{1} = groups'.format(_TAB_STR * indentation, self.dict_variable_name)


class _CxPrefetchGroupsFromPatternMatch(_CxChild):
def src(self, indentation):
def src(self, indentation: int) -> str:
return '{0}groups = match.groupdict()'.format(_TAB_STR * indentation)


class _CxReturnNone(_CxChild):
def src(self, indentation):
def src(self, indentation: int) -> str:
return '{0}return None'.format(_TAB_STR * indentation)


class _CxReturnValue(_CxChild):
def __init__(self, value_idx):
def __init__(self, value_idx: int) -> None:
self._value_idx = value_idx

def src(self, indentation):
def src(self, indentation: int) -> str:
return '{0}return return_values[{1}]'.format(
_TAB_STR * indentation, self._value_idx
)


class _CxSetParamFromPath(_CxChild):
def __init__(self, param_name, segment_idx):
def __init__(self, param_name: str, segment_idx: int) -> None:
self._param_name = param_name
self._segment_idx = segment_idx

def src(self, indentation):
def src(self, indentation: int) -> str:
return "{0}params['{1}'] = path[{2}]".format(
_TAB_STR * indentation,
self._param_name,
Expand All @@ -1160,11 +1156,11 @@


class _CxSetParamFromValue(_CxChild):
def __init__(self, param_name, field_value_name):
def __init__(self, param_name: str, field_value_name: str) -> None:
self._param_name = param_name
self._field_value_name = field_value_name

def src(self, indentation):
def src(self, indentation: int) -> str:
return "{0}params['{1}'] = {2}".format(
_TAB_STR * indentation,
self._param_name,
Expand All @@ -1173,10 +1169,10 @@


class _CxSetParamsFromDict(_CxChild):
def __init__(self, dict_value_name):
def __init__(self, dict_value_name: str) -> None:
self._dict_value_name = dict_value_name

def src(self, indentation):
def src(self, indentation: int) -> str:
return '{0}params.update({1})'.format(
_TAB_STR * indentation,
self._dict_value_name,
Expand Down
Loading