1
|
|
|
from typing import Any, Optional |
2
|
|
|
|
3
|
|
|
from typish import Module |
4
|
|
|
|
5
|
|
|
|
6
|
|
View Code Duplication |
class Attribute: |
|
|
|
|
7
|
|
|
""" |
8
|
|
|
Represents an attribute of a module. |
9
|
|
|
""" |
10
|
|
|
|
11
|
|
|
def __init__( |
12
|
|
|
self, |
13
|
|
|
name: str, |
14
|
|
|
type_: type, |
15
|
|
|
value: Any, |
16
|
|
|
doc: Optional[str], |
17
|
|
|
comment: Optional[str], |
18
|
|
|
hint: Optional[str], |
19
|
|
|
module: Module, |
20
|
|
|
assigned_value: str, |
21
|
|
|
line: str, |
22
|
|
|
line_nr: int): |
23
|
|
|
""" |
24
|
|
|
Constructor. |
25
|
|
|
:param name: the name of the attribute. |
26
|
|
|
:param type_: the actual type of the attribute. |
27
|
|
|
:param value: the actual value of the attribute. |
28
|
|
|
:param doc: any docstring on top of the attribute. |
29
|
|
|
:param comment: any inline comment behind the attribute. |
30
|
|
|
:param hint: the hinted type of the attribute. |
31
|
|
|
:param module: the module that contains the attribute. |
32
|
|
|
:param assigned_value: the value that was assigned. |
33
|
|
|
:param line: the line that defines the attribute. |
34
|
|
|
:param line_nr: the line number that holds the attribute. |
35
|
|
|
""" |
36
|
|
|
self.name = name |
37
|
|
|
self.type_ = type_ |
38
|
|
|
self.value = value |
39
|
|
|
self.doc = doc |
40
|
|
|
self.comment = comment |
41
|
|
|
self.hint = hint |
42
|
|
|
self.module = module |
43
|
|
|
self.assigned_value = assigned_value |
44
|
|
|
self.line = line |
45
|
|
|
self.line_nr = line_nr |
46
|
|
|
|
47
|
|
|
@property |
48
|
|
|
def is_private(self) -> bool: |
49
|
|
|
""" |
50
|
|
|
Return whether this attribute is marked as private. |
51
|
|
|
:return: True if this attribute is supposed to be private. |
52
|
|
|
""" |
53
|
|
|
return self.name.startswith('_') |
54
|
|
|
|
55
|
|
|
@property |
56
|
|
|
def is_public(self) -> bool: |
57
|
|
|
""" |
58
|
|
|
Return whether this attribute is public. |
59
|
|
|
:return: True if this attribute is not private. |
60
|
|
|
""" |
61
|
|
|
return not self.is_private |
62
|
|
|
|
63
|
|
|
@property |
64
|
|
|
def is_constant(self) -> bool: |
65
|
|
|
""" |
66
|
|
|
Return whether this attribute is supposed to be a constant. |
67
|
|
|
:return: True when this attribute is supposed to be a constant. |
68
|
|
|
""" |
69
|
|
|
return self.name.isupper() |
70
|
|
|
|
71
|
|
|
def __eq__(self, other: object) -> bool: |
72
|
|
|
""" |
73
|
|
|
Compare this attribute with other and check if they are equal. |
74
|
|
|
:param other: another attribute instance. |
75
|
|
|
:return: True if both instances are considered to be equal. |
76
|
|
|
""" |
77
|
|
|
return (isinstance(other, Attribute) |
78
|
|
|
and other.name == self.name |
79
|
|
|
and other.value == self.value |
80
|
|
|
and other.type_ == self.type_ |
81
|
|
|
and other.module == self.module |
82
|
|
|
and other.line_nr == self.line_nr) |
83
|
|
|
|