FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pytype/pytype/context.py at main · r-o-main/pytype · GitHub
r-o-main
/
pytype
Public
forked from
google/pytype
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pytype
/
pytype
/
context.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
165 lines (146 loc) · 5.47 KB
Breadcrumbs
pytype
/
pytype
/
context.py
Copy path
File metadata and controls
165 lines (146 loc) · 5.47 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""Container of things that should be accessible to all abstract values."""
import
contextlib
import
logging
from
typing
import
Dict
,
List
,
Tuple
from
pytype
import
annotation_utils
from
pytype
import
attribute
from
pytype
import
config
from
pytype
import
convert
from
pytype
import
errors
from
pytype
import
load_pytd
from
pytype
import
matcher
from
pytype
import
output
from
pytype
import
special_builtins
from
pytype
import
tracer_vm
from
pytype
import
vm_utils
from
pytype
.
abstract
import
abstract
from
pytype
.
abstract
import
abstract_utils
from
pytype
.
typegraph
import
cfg
from
pytype
.
typegraph
import
cfg_utils
log
=
logging
.
getLogger
(
__name__
)
class
Context
:
"""An abstract context."""
def
__init__
(
self
,
options
:
config
.
Options
,
loader
:
load_pytd
.
Loader
,
generate_unknowns
:
bool
=
False
,
store_all_calls
:
bool
=
False
,
):
# Inputs
self
.
options
=
options
self
.
python_version
:
Tuple
[
int
,
int
]
=
self
.
options
.
python_version
self
.
loader
=
loader
self
.
generate_unknowns
=
generate_unknowns
self
.
store_all_calls
=
store_all_calls
# Typegraph
self
.
program
=
cfg
.
Program
()
self
.
root_node
:
cfg
.
CFGNode
=
self
.
program
.
NewCFGNode
(
"root"
)
self
.
program
.
entrypoint
=
self
.
root_node
# Represents the program exit. Needs to be set before analyze_types.
self
.
exitpoint
:
cfg
.
CFGNode
=
None
# Helper classes/modules
self
.
vm
=
tracer_vm
.
CallTracer
(
self
)
self
.
errorlog
=
errors
.
ErrorLog
()
self
.
annotation_utils
=
annotation_utils
.
AnnotationUtils
(
self
)
self
.
attribute_handler
=
attribute
.
AbstractAttributeHandler
(
self
)
self
.
converter_minimally_initialized
=
False
self
.
convert
=
convert
.
Converter
(
self
)
self
.
pytd_convert
=
output
.
Converter
(
self
)
self
.
program
.
default_data
=
self
.
convert
.
unsolvable
# Other context
self
.
callself_stack
:
List
[
cfg
.
Variable
]
=
[]
# Map from builtin names to canonical objects.
self
.
special_builtins
:
Dict
[
str
,
abstract
.
BaseValue
]
=
{
# The super() function.
"super"
:
self
.
convert
.
super_type
,
# The object type.
"object"
:
self
.
convert
.
object_type
,
# for more pretty branching tests.
"__random__"
:
self
.
convert
.
primitive_class_instances
[
bool
],
# for debugging
"reveal_type"
:
special_builtins
.
RevealType
(
self
),
# boolean values.
"True"
:
self
.
convert
.
true
,
"False"
:
self
.
convert
.
false
,
# builtin classes
"property"
:
special_builtins
.
Property
(
self
),
"staticmethod"
:
special_builtins
.
StaticMethod
(
self
),
"classmethod"
:
special_builtins
.
ClassMethod
(
self
),
"dict"
:
special_builtins
.
Dict
(
self
),
}
# builtin functions
for
cls
in
(
special_builtins
.
Abs
,
special_builtins
.
AssertType
,
special_builtins
.
HasAttr
,
special_builtins
.
IsCallable
,
special_builtins
.
IsInstance
,
special_builtins
.
IsSubclass
,
special_builtins
.
Next
,
):
self
.
special_builtins
[
cls
.
name
]
=
cls
.
make
(
self
)
# If set, allow construction of recursive values, setting the
# self-referential field to Any
self
.
recursion_allowed
=
False
def
matcher
(
self
,
node
):
return
matcher
.
AbstractMatcher
(
node
,
self
)
@
contextlib
.
contextmanager
def
allow_recursive_convert
(
self
):
old
=
self
.
recursion_allowed
self
.
recursion_allowed
=
True
try
:
yield
finally
:
self
.
recursion_allowed
=
old
def
new_unsolvable
(
self
,
node
):
"""Create a new unsolvable variable at node."""
return
self
.
convert
.
unsolvable
.
to_variable
(
node
)
def
join_cfg_nodes
(
self
,
nodes
):
"""Get a new node to which the given nodes have been joined."""
assert
nodes
if
len
(
nodes
)
==
1
:
return
nodes
[
0
]
else
:
ret
=
self
.
program
.
NewCFGNode
(
self
.
vm
.
frame
and
self
.
vm
.
frame
.
current_opcode
and
self
.
vm
.
frame
.
current_opcode
.
line
)
for
node
in
nodes
:
node
.
ConnectTo
(
ret
)
return
ret
def
join_variables
(
self
,
node
,
variables
):
return
cfg_utils
.
merge_variables
(
self
.
program
,
node
,
variables
)
def
join_bindings
(
self
,
node
,
bindings
):
return
cfg_utils
.
merge_bindings
(
self
.
program
,
node
,
bindings
)
# TODO(b/202335303): The only reason this method exists is so that
# abstract/mixin.py can construct an abstract.NativeFunction.
def
make_native_function
(
self
,
name
,
method
):
return
abstract
.
NativeFunction
(
name
,
method
,
self
)
def
make_class
(
self
,
node
,
props
):
return
vm_utils
.
make_class
(
node
,
props
,
self
)
def
check_annotation_type_mismatch
(
self
,
node
,
name
,
typ
,
value
,
stack
,
allow_none
,
details
=
None
):
"""Checks for a mismatch between a variable's annotation and value.
Args:
node: node
name: variable name
typ: variable annotation
value: variable value
stack: a frame stack for error reporting
allow_none: whether a value of None is allowed for any type
details: any additional details to add to the error message
"""
if
not
typ
or
not
value
:
return
if
(
value
.
data
==
[
self
.
convert
.
ellipsis
]
or
allow_none
and
value
.
data
==
[
self
.
convert
.
none
]):
return
contained_type
=
abstract_utils
.
match_type_container
(
typ
, (
"typing.ClassVar"
,
"dataclasses.InitVar"
))
if
contained_type
:
typ
=
contained_type
bad
=
self
.
matcher
(
node
).
bad_matches
(
value
,
typ
)
for
view
,
error_details
in
bad
:
binding
=
view
[
value
]
self
.
errorlog
.
annotation_type_mismatch
(
stack
,
typ
,
binding
,
name
,
error_details
,
details
)
Back
|
FazBrowse Home
|
New Git URL