Skip to content

Commit 7b653b3

Browse files
committed
feat: Parse attribute values, parameter defaults and decorators as expressions
This commit refactors how we build expressions (visiting AST nodes) to make it more performant (reduced memory consumption) while handling the different possible scenarios (future annotations or not, trying to reparse strings or not). Building values, defaults and decorators as expressions allows us expression features (full name resolution) within the visitor. This will also allow us to render cross-references in mkdocstrings.
1 parent d7f314a commit 7b653b3

12 files changed

Lines changed: 642 additions & 471 deletions

File tree

config/ruff.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ ignore = [
8585
"S603", # `subprocess` call: check for execution of untrusted input
8686
"S607", # Starting a process with a partial executable path
8787
]
88-
"src/*/*/nodes.py" = [
88+
"src/*/*/nodes/*.py" = [
89+
"ARG001", # Unused function argument
8990
"N812", # Lowercase `keyword` imported as non-lowercase `NodeKeyword`
9091
]
9192
"scripts/*.py" = [

docs/schema.json

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,9 @@
223223
"type": "object",
224224
"properties": {
225225
"value": {
226-
"title": "The decorator, as a string.",
226+
"title": "The decorator value (string, name or expression).",
227227
"markdownDescription": "https://mkdocstrings.github.io/griffe/reference/griffe/dataclasses/#griffe.dataclasses.Decorator.value",
228-
"type": "string"
228+
"$ref": "#/$defs/annotation"
229229
},
230230
"lineno": {
231231
"title": "The decorator starting line number.",
@@ -291,10 +291,7 @@
291291
"default": {
292292
"title": "The default value of the parameter.",
293293
"markdownDescription": "https://mkdocstrings.github.io/griffe/reference/griffe/dataclasses/#griffe.dataclasses.Parameter.default",
294-
"type": [
295-
"string",
296-
"null"
297-
]
294+
"$ref": "#/$defs/annotation"
298295
}
299296
},
300297
"required": [
@@ -328,7 +325,7 @@
328325
"value": {
329326
"title": "For attributes, their value.",
330327
"markdownDescription": "https://mkdocstrings.github.io/griffe/reference/griffe/dataclasses/#griffe.dataclasses.Attribute.value",
331-
"type": "string"
328+
"$ref": "#/$defs/annotation"
332329
},
333330
"annotation": {
334331
"title": "For attributes, their type annotation.",
@@ -342,7 +339,7 @@
342339
}
343340
],
344341
"$defs": {
345-
"annotation_name": {
342+
"name": {
346343
"type": "object",
347344
"properties": {
348345
"source": {
@@ -362,18 +359,18 @@
362359
"full"
363360
]
364361
},
365-
"annotation_expression": {
362+
"expression": {
366363
"type": "array",
367364
"items": {
368365
"oneOf": [
369366
{
370367
"type": "string"
371368
},
372369
{
373-
"$ref": "#/$defs/annotation_name"
370+
"$ref": "#/$defs/name"
374371
},
375372
{
376-
"$ref": "#/$defs/annotation_expression"
373+
"$ref": "#/$defs/expression"
377374
}
378375
]
379376
}
@@ -384,13 +381,13 @@
384381
"type": "null"
385382
},
386383
{
387-
"const": "None"
384+
"type": "string"
388385
},
389386
{
390-
"$ref": "#/$defs/annotation_name"
387+
"$ref": "#/$defs/name"
391388
},
392389
{
393-
"$ref": "#/$defs/annotation_expression"
390+
"$ref": "#/$defs/expression"
394391
}
395392
]
396393
}

src/griffe/agents/nodes/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
)
2828
from griffe.agents.nodes._imports import relative_to_absolute
2929
from griffe.agents.nodes._names import get_instance_names, get_name, get_names
30-
from griffe.agents.nodes._parameters import get_parameter_default
30+
from griffe.agents.nodes._parameters import get_call_keyword_arguments
3131
from griffe.agents.nodes._runtime import ObjectKind, ObjectNode
3232
from griffe.agents.nodes._values import get_value, safe_get_value
3333

@@ -44,13 +44,13 @@
4444
"get__all__",
4545
"get_annotation",
4646
"get_base_class",
47+
"get_call_keyword_arguments",
4748
"get_condition",
4849
"get_docstring",
4950
"get_expression",
5051
"get_instance_names",
5152
"get_name",
5253
"get_names",
53-
"get_parameter_default",
5454
"get_value",
5555
"ObjectKind",
5656
"ObjectNode",

src/griffe/agents/nodes/_all.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
logger = get_logger(__name__)
2828

2929

30-
def _extract_constant(node: NodeConstant, parent: Module) -> list[str | Name]: # noqa: ARG001
30+
def _extract_constant(node: NodeConstant, parent: Module) -> list[str | Name]:
3131
return [node.value]
3232

3333

0 commit comments

Comments
 (0)