| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d9c1435 commit bf876f8
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -137,6 +137,9 @@ def _attrs(self): | |||
| 137 | 137 | def instantiate(self) -> 'Singleton': | |
| 138 | 138 | return self | |
| 139 | 139 | ||
| 140 | + def get_attribute(self, name: str) -> 'Singleton': | ||
| 141 | + return self | ||
| 142 | + | ||
| 140 | 143 | ||
| 141 | 144 | class Union(BaseValue): | |
| 142 | 145 | """Union of values.""" | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -110,7 +110,7 @@ def instantiate(self) -> 'FrozenInstance': | |||
| 110 | 110 | if isinstance(setup_method, functions_lib.InterpreterFunction): | |
| 111 | 111 | _ = setup_method.bind_to(self).analyze() | |
| 112 | 112 | constructor = self.get_attribute(self.constructor) | |
| 113 | - if constructor: | ||
| 113 | + if constructor and constructor.full_name != 'builtins.object.__new__': | ||
| 114 | 114 | log.error('Custom __new__ not yet implemented') | |
| 115 | 115 | instance = MutableInstance(self._ctx, self) | |
| 116 | 116 | for initializer_name in self.initializers: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -596,8 +596,8 @@ def _attrs(self): | |||
| 596 | 596 | return (self.name, self.code) | |
| 597 | 597 | ||
| 598 | 598 | def call_with_mapped_args(self, mapped_args: MappedArgs[_FrameT]) -> _FrameT: | |
| 599 | - log.info('Calling function:\n Sig: %s\n Args: %s', | ||
| 600 | - mapped_args.signature, mapped_args.argdict) | ||
| 599 | + log.info('Calling function %s:\n Sig: %s\n Args: %s', | ||
| 600 | + self.full_name, mapped_args.signature, mapped_args.argdict) | ||
| 601 | 601 | parent_frame = mapped_args.frame or self._parent_frame | |
| 602 | 602 | if parent_frame.final_locals is None: | |
| 603 | 603 | k = None | |
@@ -622,6 +622,8 @@ class PytdFunction(SimpleFunction[SimpleReturn]): | |||
| 622 | 622 | ||
| 623 | 623 | def call_with_mapped_args( | |
| 624 | 624 | self, mapped_args: MappedArgs[FrameType]) -> SimpleReturn: | |
| 625 | + log.info('Calling function %s:\n Sig: %s\n Args: %s', | ||
| 626 | + self.full_name, mapped_args.signature, mapped_args.argdict) | ||
| 625 | 627 | ret = mapped_args.signature.annotations['return'].instantiate() | |
| 626 | 628 | return SimpleReturn(ret) | |
| 627 | 629 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,7 @@ | |||
| 1 | 1 | """Conversion from pytd to abstract representations of Python values.""" | |
| 2 | 2 | ||
| 3 | + from typing import Optional, Tuple | ||
| 4 | + | ||
| 3 | 5 | from pytype.pytd import pytd | |
| 4 | 6 | from pytype.rewrite.abstract import abstract | |
| 5 | 7 | ||
@@ -38,8 +40,10 @@ def pytd_class_to_value(self, cls: pytd.Class) -> abstract.SimpleClass: | |||
| 38 | 40 | # don't cause infinite recursion. | |
| 39 | 41 | self._cache.classes[cls] = abstract_class | |
| 40 | 42 | for method in cls.methods: | |
| 41 | - abstract_class.members[method.name] = ( | ||
| 42 | - self.pytd_function_to_value(method)) | ||
| 43 | + # For consistency with InterpreterFunction, prepend the class name. | ||
| 44 | + full_name = f'{name}.{method.name}' | ||
| 45 | + method_value = self.pytd_function_to_value(method, (module, full_name)) | ||
| 46 | + abstract_class.members[method.name] = method_value | ||
| 43 | 47 | for constant in cls.constants: | |
| 44 | 48 | constant_type = self.pytd_type_to_value(constant.type) | |
| 45 | 49 | abstract_class.members[constant.name] = constant_type.instantiate() | |
@@ -61,11 +65,15 @@ def pytd_class_to_value(self, cls: pytd.Class) -> abstract.SimpleClass: | |||
| 61 | 65 | return abstract_class | |
| 62 | 66 | ||
| 63 | 67 | def pytd_function_to_value( | |
| 64 | - self, func: pytd.Function) -> abstract.PytdFunction: | ||
| 68 | + self, func: pytd.Function, func_name: Optional[Tuple[str, str]] = None, | ||
| 69 | + ) -> abstract.PytdFunction: | ||
| 65 | 70 | """Converts a pytd function to an abstract function.""" | |
| 66 | 71 | if func in self._cache.funcs: | |
| 67 | 72 | return self._cache.funcs[func] | |
| 68 | - module, _, name = func.name.rpartition('.') | ||
| 73 | + if func_name: | ||
| 74 | + module, name = func_name | ||
| 75 | + else: | ||
| 76 | + module, _, name = func.name.rpartition('.') | ||
| 69 | 77 | signatures = tuple( | |
| 70 | 78 | abstract.Signature.from_pytd(self._ctx, name, pytd_sig) | |
| 71 | 79 | for pytd_sig in func.signatures) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -65,7 +65,8 @@ def f(self, x) -> None: ... | |||
| 65 | 65 | self.assertEqual(set(cls.members), {'f'}) | |
| 66 | 66 | f = cls.members['f'] | |
| 67 | 67 | self.assertIsInstance(f, abstract.PytdFunction) | |
| 68 | - self.assertEqual(repr(f.signatures[0]), 'def f(self: C, x: Any) -> None') | ||
| 68 | + self.assertEqual(f.module, '<test>') | ||
| 69 | + self.assertEqual(repr(f.signatures[0]), 'def C.f(self: C, x: Any) -> None') | ||
| 69 | 70 | ||
| 70 | 71 | def test_constant(self): | |
| 71 | 72 | pytd_cls = self.build_pytd(""" | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,6 @@ | |||
| 1 | 1 | """A frame of an abstract VM for type analysis of python bytecode.""" | |
| 2 | 2 | ||
| 3 | + import itertools | ||
| 3 | 4 | import logging | |
| 4 | 5 | from typing import Any, FrozenSet, List, Mapping, Optional, Sequence, Set, Type | |
| 5 | 6 | ||
@@ -310,7 +311,8 @@ def _merge_nonlocals_into(self, frame: Optional['Frame']) -> None: | |||
| 310 | 311 | ||
| 311 | 312 | def _build_class(self, args: abstract.Args) -> abstract.InterpreterClass: | |
| 312 | 313 | builder = args.posargs[0].get_atomic_value(_FrameFunction) | |
| 313 | - name = abstract.get_atomic_constant(args.posargs[1], str) | ||
| 314 | + name_var = args.posargs[1] | ||
| 315 | + name = abstract.get_atomic_constant(name_var, str) | ||
| 314 | 316 | ||
| 315 | 317 | base_vars = args.posargs[2:] | |
| 316 | 318 | bases = [] | |
@@ -330,16 +332,41 @@ def _build_class(self, args: abstract.Args) -> abstract.InterpreterClass: | |||
| 330 | 332 | keywords[kw] = val | |
| 331 | 333 | ||
| 332 | 334 | frame = builder.call(abstract.Args(frame=self)) | |
| 333 | - cls = abstract.InterpreterClass( | ||
| 334 | - ctx=self._ctx, | ||
| 335 | - name=name, | ||
| 336 | - members=dict(frame.final_locals), | ||
| 337 | - bases=bases, | ||
| 338 | - keywords=keywords, | ||
| 339 | - functions=frame.functions, | ||
| 340 | - classes=frame.classes, | ||
| 341 | - ) | ||
| 342 | - log.info('Created class: %s', cls.name) | ||
| 335 | + members = dict(frame.final_locals) | ||
| 336 | + metaclass_instance = None | ||
| 337 | + for metaclass in itertools.chain([keywords.get('metaclass')], | ||
| 338 | + (base.metaclass for base in bases)): | ||
| 339 | + if not metaclass: | ||
| 340 | + continue | ||
| 341 | + metaclass_new = metaclass.get_attribute('__new__') | ||
| 342 | + if metaclass_new.full_name == 'builtins.type.__new__': | ||
| 343 | + continue | ||
| 344 | + # The metaclass has overridden type.__new__. Invoke the custom __new__ | ||
| 345 | + # method to construct the class. | ||
| 346 | + metaclass_var = metaclass.to_variable() | ||
| 347 | + bases_var = abstract.Tuple(self._ctx, tuple(base_vars)).to_variable() | ||
| 348 | + members_var = abstract.Dict( | ||
| 349 | + self._ctx, {self._ctx.consts[k].to_variable(): v.to_variable() | ||
| 350 | + for k, v in members.items()} | ||
| 351 | + ).to_variable() | ||
| 352 | + args = abstract.Args( | ||
| 353 | + posargs=(metaclass_var, name_var, bases_var, members_var), | ||
| 354 | + frame=self) | ||
| 355 | + metaclass_instance = metaclass_new.call(args).get_return_value() | ||
| 356 | + break | ||
| 357 | + if metaclass_instance and metaclass_instance.full_name == name: | ||
| 358 | + cls = metaclass_instance | ||
| 359 | + else: | ||
| 360 | + cls = abstract.InterpreterClass( | ||
| 361 | + ctx=self._ctx, | ||
| 362 | + name=name, | ||
| 363 | + members=members, | ||
| 364 | + bases=bases, | ||
| 365 | + keywords=keywords, | ||
| 366 | + functions=frame.functions, | ||
| 367 | + classes=frame.classes, | ||
| 368 | + ) | ||
| 369 | + log.info('Created class: %r', cls) | ||
| 343 | 370 | return cls | |
| 344 | 371 | ||
| 345 | 372 | def _call_function( | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -142,5 +142,29 @@ def test_aliases(self): | |||
| 142 | 142 | """) | |
| 143 | 143 | ||
| 144 | 144 | ||
| 145 | + @test_base.skip('Under construction') | ||
| 146 | + class EnumTest(RewriteTest): | ||
| 147 | + """Enum tests.""" | ||
| 148 | + | ||
| 149 | + def test_member(self): | ||
| 150 | + self.Check(""" | ||
| 151 | + import enum | ||
| 152 | + class E(enum.Enum): | ||
| 153 | + X = 42 | ||
| 154 | + assert_type(E.X, E) | ||
| 155 | + """) | ||
| 156 | + | ||
| 157 | + def test_member_pyi(self): | ||
| 158 | + with self.DepTree([('foo.pyi', """ | ||
| 159 | + import enum | ||
| 160 | + class E(enum.Enum): | ||
| 161 | + X = 42 | ||
| 162 | + """)]): | ||
| 163 | + self.Check(""" | ||
| 164 | + import foo | ||
| 165 | + assert_type(foo.E.X, foo.E) | ||
| 166 | + """) | ||
| 167 | + | ||
| 168 | + | ||
| 145 | 169 | if __name__ == '__main__': | |
| 146 | 170 | test_base.main() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,4 @@ | |||
| 1 | - from typing import Any, Dict, Iterable, Iterator, Tuple, Type, TypeVar, Union | ||
| 1 | + from typing import Any, Dict, Iterable, Iterator, Self, Tuple, Type, TypeVar, Union | ||
| 2 | 2 | ||
| 3 | 3 | _T = TypeVar('_T') | |
| 4 | 4 | _EnumType = TypeVar('_EnumType', bound=Type[Enum]) | |
@@ -8,6 +8,9 @@ class EnumMeta(type, Iterable): | |||
| 8 | 8 | def __getitem__(cls: EnumMeta, name: str) -> Any: ... | |
| 9 | 9 | def __contains__(self, member: Enum) -> bool: ... | |
| 10 | 10 | def __len__(self) -> int: ... | |
| 11 | + def __new__( | ||
| 12 | + metacls: type[Self], cls: str, bases: tuple[type, ...], classdict: dict[str, Any], **kwds: Any | ||
| 13 | + ) -> Self: ... | ||
| 11 | 14 | ||
| 12 | 15 | class Enum(metaclass=EnumMeta): | |
| 13 | 16 | __members__: collections.OrderedDict[str, Enum] | |
| Back | FazBrowse Home | New Git URL |
0 commit comments