Method: pylint.pyreverse.utils.get_annotation
Calls: 405, Exceptions: 36, Paths: 10Back
Path 1: 291 calls (0.72)
AssignName (291)
None (291)
1def get_annotation(
2 node: nodes.AssignAttr | nodes.AssignName,
3) -> nodes.Name | nodes.Subscript | None:
4 """Return the annotation for `node`."""
5 ann = None
6 if isinstance(node.parent, nodes.AnnAssign):
7 ann = node.parent.annotation
8 elif isinstance(node, nodes.AssignAttr):
9 init_method = node.parent.parent
10 try:
11 annotations = dict(zip(init_method.locals, init_method.args.annotations))
12 ann = annotations.get(node.parent.value.name)
13 except AttributeError:
14 pass
15 else:
16 return ann
17
18 try:
19 default, *_ = node.infer()
20 except astroid.InferenceError:
21 default = ""
22
23 label = get_annotation_label(ann)
24 if ann:
25 label = (
26 rf"Optional[{label}]"
27 if getattr(default, "value", "value") is None
28 and not label.startswith("Optional")
29 else label
30 )
31 if label:
32 ann.name = label
33 return ann
Path 2: 28 calls (0.07)
AssignAttr (16) AssignName (10) Const (2)
Subscript (19) Name (6) BinOp (3)
1def get_annotation(
2 node: nodes.AssignAttr | nodes.AssignName,
3) -> nodes.Name | nodes.Subscript | None:
4 """Return the annotation for `node`."""
5 ann = None
6 if isinstance(node.parent, nodes.AnnAssign):
7 ann = node.parent.annotation
8 elif isinstance(node, nodes.AssignAttr):
9 init_method = node.parent.parent
10 try:
11 annotations = dict(zip(init_method.locals, init_method.args.annotations))
12 ann = annotations.get(node.parent.value.name)
13 except AttributeError:
14 pass
15 else:
16 return ann
17
18 try:
19 default, *_ = node.infer()
20 except astroid.InferenceError:
21 default = ""
22
23 label = get_annotation_label(ann)
24 if ann:
25 label = (
26 rf"Optional[{label}]"
27 if getattr(default, "value", "value") is None
28 and not label.startswith("Optional")
29 else label
30 )
31 if label:
32 ann.name = label
33 return ann
Path 3: 24 calls (0.06)
AssignAttr (24)
None (24)
1def get_annotation(
2 node: nodes.AssignAttr | nodes.AssignName,
3) -> nodes.Name | nodes.Subscript | None:
4 """Return the annotation for `node`."""
5 ann = None
6 if isinstance(node.parent, nodes.AnnAssign):
7 ann = node.parent.annotation
8 elif isinstance(node, nodes.AssignAttr):
9 init_method = node.parent.parent
10 try:
11 annotations = dict(zip(init_method.locals, init_method.args.annotations))
12 ann = annotations.get(node.parent.value.name)
13 except AttributeError:
14 pass
15 else:
16 return ann
17
18 try:
19 default, *_ = node.infer()
20 except astroid.InferenceError:
21 default = ""
22
23 label = get_annotation_label(ann)
24 if ann:
25 label = (
26 rf"Optional[{label}]"
27 if getattr(default, "value", "value") is None
28 and not label.startswith("Optional")
29 else label
30 )
31 if label:
32 ann.name = label
33 return ann
Path 4: 20 calls (0.05)
AssignAttr (20)
None (20)
AttributeError (20)
1def get_annotation(
2 node: nodes.AssignAttr | nodes.AssignName,
3) -> nodes.Name | nodes.Subscript | None:
4 """Return the annotation for `node`."""
5 ann = None
6 if isinstance(node.parent, nodes.AnnAssign):
7 ann = node.parent.annotation
8 elif isinstance(node, nodes.AssignAttr):
9 init_method = node.parent.parent
10 try:
11 annotations = dict(zip(init_method.locals, init_method.args.annotations))
12 ann = annotations.get(node.parent.value.name)
13 except AttributeError:
14 pass
15 else:
16 return ann
17
18 try:
19 default, *_ = node.infer()
20 except astroid.InferenceError:
21 default = ""
22
23 label = get_annotation_label(ann)
24 if ann:
25 label = (
26 rf"Optional[{label}]"
27 if getattr(default, "value", "value") is None
28 and not label.startswith("Optional")
29 else label
30 )
31 if label:
32 ann.name = label
33 return ann
Path 5: 16 calls (0.04)
AssignAttr (16)
None (16)
AttributeError (16)
1def get_annotation(
2 node: nodes.AssignAttr | nodes.AssignName,
3) -> nodes.Name | nodes.Subscript | None:
4 """Return the annotation for `node`."""
5 ann = None
6 if isinstance(node.parent, nodes.AnnAssign):
7 ann = node.parent.annotation
8 elif isinstance(node, nodes.AssignAttr):
9 init_method = node.parent.parent
10 try:
11 annotations = dict(zip(init_method.locals, init_method.args.annotations))
12 ann = annotations.get(node.parent.value.name)
13 except AttributeError:
14 pass
15 else:
16 return ann
17
18 try:
19 default, *_ = node.infer()
20 except astroid.InferenceError:
21 default = ""
22
23 label = get_annotation_label(ann)
24 if ann:
25 label = (
26 rf"Optional[{label}]"
27 if getattr(default, "value", "value") is None
28 and not label.startswith("Optional")
29 else label
30 )
31 if label:
32 ann.name = label
33 return ann
Path 6: 12 calls (0.03)
AssignAttr (12)
Name (10) Subscript (2)
1def get_annotation(
2 node: nodes.AssignAttr | nodes.AssignName,
3) -> nodes.Name | nodes.Subscript | None:
4 """Return the annotation for `node`."""
5 ann = None
6 if isinstance(node.parent, nodes.AnnAssign):
7 ann = node.parent.annotation
8 elif isinstance(node, nodes.AssignAttr):
9 init_method = node.parent.parent
10 try:
11 annotations = dict(zip(init_method.locals, init_method.args.annotations))
12 ann = annotations.get(node.parent.value.name)
13 except AttributeError:
14 pass
15 else:
16 return ann
17
18 try:
19 default, *_ = node.infer()
20 except astroid.InferenceError:
21 default = ""
22
23 label = get_annotation_label(ann)
24 if ann:
25 label = (
26 rf"Optional[{label}]"
27 if getattr(default, "value", "value") is None
28 and not label.startswith("Optional")
29 else label
30 )
31 if label:
32 ann.name = label
33 return ann
Path 7: 5 calls (0.01)
AssignAttr (5)
Subscript (4) Name (1)
1def get_annotation(
2 node: nodes.AssignAttr | nodes.AssignName,
3) -> nodes.Name | nodes.Subscript | None:
4 """Return the annotation for `node`."""
5 ann = None
6 if isinstance(node.parent, nodes.AnnAssign):
7 ann = node.parent.annotation
8 elif isinstance(node, nodes.AssignAttr):
9 init_method = node.parent.parent
10 try:
11 annotations = dict(zip(init_method.locals, init_method.args.annotations))
12 ann = annotations.get(node.parent.value.name)
13 except AttributeError:
14 pass
15 else:
16 return ann
17
18 try:
19 default, *_ = node.infer()
20 except astroid.InferenceError:
21 default = ""
22
23 label = get_annotation_label(ann)
24 if ann:
25 label = (
26 rf"Optional[{label}]"
27 if getattr(default, "value", "value") is None
28 and not label.startswith("Optional")
29 else label
30 )
31 if label:
32 ann.name = label
33 return ann
Path 8: 4 calls (0.01)
AssignAttr (3) Const (1)
Subscript (3) Name (1)
1def get_annotation(
2 node: nodes.AssignAttr | nodes.AssignName,
3) -> nodes.Name | nodes.Subscript | None:
4 """Return the annotation for `node`."""
5 ann = None
6 if isinstance(node.parent, nodes.AnnAssign):
7 ann = node.parent.annotation
8 elif isinstance(node, nodes.AssignAttr):
9 init_method = node.parent.parent
10 try:
11 annotations = dict(zip(init_method.locals, init_method.args.annotations))
12 ann = annotations.get(node.parent.value.name)
13 except AttributeError:
14 pass
15 else:
16 return ann
17
18 try:
19 default, *_ = node.infer()
20 except astroid.InferenceError:
21 default = ""
22
23 label = get_annotation_label(ann)
24 if ann:
25 label = (
26 rf"Optional[{label}]"
27 if getattr(default, "value", "value") is None
28 and not label.startswith("Optional")
29 else label
30 )
31 if label:
32 ann.name = label
33 return ann
Path 9: 3 calls (0.01)
AssignName (1) AssignAttr (1) Const (1)
Name (2) BinOp (1)
1def get_annotation(
2 node: nodes.AssignAttr | nodes.AssignName,
3) -> nodes.Name | nodes.Subscript | None:
4 """Return the annotation for `node`."""
5 ann = None
6 if isinstance(node.parent, nodes.AnnAssign):
7 ann = node.parent.annotation
8 elif isinstance(node, nodes.AssignAttr):
9 init_method = node.parent.parent
10 try:
11 annotations = dict(zip(init_method.locals, init_method.args.annotations))
12 ann = annotations.get(node.parent.value.name)
13 except AttributeError:
14 pass
15 else:
16 return ann
17
18 try:
19 default, *_ = node.infer()
20 except astroid.InferenceError:
21 default = ""
22
23 label = get_annotation_label(ann)
24 if ann:
25 label = (
26 rf"Optional[{label}]"
27 if getattr(default, "value", "value") is None
28 and not label.startswith("Optional")
29 else label
30 )
31 if label:
32 ann.name = label
33 return ann
Path 10: 2 calls (0.0)
AssignAttr (2)
Name (2)
1def get_annotation(
2 node: nodes.AssignAttr | nodes.AssignName,
3) -> nodes.Name | nodes.Subscript | None:
4 """Return the annotation for `node`."""
5 ann = None
6 if isinstance(node.parent, nodes.AnnAssign):
7 ann = node.parent.annotation
8 elif isinstance(node, nodes.AssignAttr):
9 init_method = node.parent.parent
10 try:
11 annotations = dict(zip(init_method.locals, init_method.args.annotations))
12 ann = annotations.get(node.parent.value.name)
13 except AttributeError:
14 pass
15 else:
16 return ann
17
18 try:
19 default, *_ = node.infer()
20 except astroid.InferenceError:
21 default = ""
22
23 label = get_annotation_label(ann)
24 if ann:
25 label = (
26 rf"Optional[{label}]"
27 if getattr(default, "value", "value") is None
28 and not label.startswith("Optional")
29 else label
30 )
31 if label:
32 ann.name = label
33 return ann