Skip to content

Commit

Permalink
Merge pull request #992 from google/google_sync
Browse files Browse the repository at this point in the history
Google sync
  • Loading branch information
rchen152 authored Aug 24, 2021
2 parents 173d9f0 + bf8b234 commit 9c96957
Show file tree
Hide file tree
Showing 20 changed files with 72 additions and 222 deletions.
8 changes: 2 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,8 @@ dependencies.
make use of ninja as the CMake generated build system. NOTE: if you
have a [ninja Python distribution](https://pypi.org/project/ninja/) installed
and active, you can skip installing the official ninja distribution.
6. __Python2.7 and Python3.x Interpreters__: A large subset of Pytype's
functional tests analyse the target (the Python source code that is being
analyzed by Pytype) twice: once as if it were Python2.7 code, and another
time as if it were in the Python 3.x version that pytype is running under.
Hence, to run these tests, you will need Python 2.7 and Python 3.x
(preferably 3.6+) interpreters installed on your system.
6. __Python3.x Interpreter__: You will need to install an interpreter for a
Python version that pytype can run under (see [Requirements](README.md#requirements)).

Required Python packages are listed in the [requirements.txt](requirements.txt)
file in this repository. They can be installed with pip with the following
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ merge-pyi -i <filepath>.py .pytype/pyi/<filename>.pyi

You need a Python 3.6-3.8 interpreter to run pytype, as well as an
interpreter in `$PATH` for the Python version of the code you're analyzing
(supported: 2.7, 3.5-3.8).
(supported: 3.5-3.8).

Platform support:

Expand Down
14 changes: 7 additions & 7 deletions docs/developers/bytecode.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ freshness: { owner: 'mdemello' reviewed: '2021-08-09' }
* [Host and Target Versions](#host-and-target-versions)
* [Opcodes](#opcodes)

<!-- Added by: rechen, at: 2021-08-10T21:18-07:00 -->
<!-- Added by: rechen, at: 2021-08-23T15:10-07:00 -->

<!--te-->

Expand Down Expand Up @@ -44,8 +44,8 @@ between the **host** and the **target** python version:

If the host and target versions differ, we need to compile python source files
to bytecode using a *target-version* interpreter, e.g. if we are running under
python 3.6 and are passed `--version=2.7` we cannot use python 3.6's internal
libraries to compile the code; we have to launch a python 2.7 interpreter,
python 3.7 and are passed `--version=3.6` we cannot use python 3.7's internal
libraries to compile the code; we have to launch a python 3.6 interpreter,
compile the target code to bytecode, and then retrieve that bytecode to run
through our VirtualMachine.

Expand Down Expand Up @@ -110,10 +110,10 @@ associated argument, and that that argument is an index into the list of names.
After defining a class for every python opcode, `opcodes.py` defines a series of
tables mapping between bytecodes and opcodes for each python version we support
(currently `2.7` and `3.5`-`3.8`). The `opcodes.py/dis()` function gets the
right mapping table for the target python version, and then passes it to a
bytecode reader which iterates over the block of bytes, converting each one into
an opcode or into the argument for the preceding opcode.
(currently `3.5`-`3.8`). The `opcodes.py/dis()` function gets the right mapping
table for the target python version, and then passes it to bytecode reader which
iterates over the block of bytes, converting each one into an opcode or into the
argument for the preceding opcode.
NOTE: One subtlety is that from python 3.6 the bytecode format changed;
therefore we need two separate bytecode scanners, `_bytecode_reader` for python
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ merge-pyi -i <filepath>.py .pytype/pyi/<filename>.pyi

You need a Python 3.6-3.8 interpreter to run pytype, as well as an
interpreter in `$PATH` for the Python version of the code you're analyzing
(supported: 2.7, 3.5-3.8).
(supported: 3.5-3.8).

Platform support:

Expand Down
11 changes: 6 additions & 5 deletions pytype/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
"builtins.list", "builtins.set", "builtins.frozenset"})


class CmpTypeError(Exception):
"""Comparing incompatible primitive constants."""


def _incompatible(left_name, right_name):
"""Incompatible primitive types can never be equal."""
if left_name == right_name:
Expand Down Expand Up @@ -47,11 +51,8 @@ def _is_equality_cmp(op):
def _compare_constants(op, left, right):
try:
return slots.COMPARES[op](left, right)
except TypeError:
# TODO(b/195453869): In host Python 3, some types are not comparable; e.g.,
# `3 < ""` leads to a type error. We should log an error now that we don't
# need to support target PY2 behaviour any more.
return None
except TypeError as e:
raise CmpTypeError() from e


def _compare_primitive_constant(vm, op, left, right):
Expand Down
2 changes: 1 addition & 1 deletion pytype/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def add_basic_options(o):
o.add_argument(
"-V", "--python_version", type=str, action="store",
dest="python_version", default=None,
help=("Python version to emulate (\"major.minor\", e.g. \"2.7\")"))
help=("Python version to emulate (\"major.minor\", e.g. \"3.7\")"))
o.add_argument(
"--strict-import", action="store_true",
dest="strict_import", default=False,
Expand Down
16 changes: 11 additions & 5 deletions pytype/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,11 +931,17 @@ def unsupported_operands(self, stack, operator, var1, var2):

@_error_name("unsupported-operands")
def _unsupported_operands(self, stack, operator, *operands, details=None):
self.error(
stack, "unsupported operand type(s) for %s: %s" % (
slots.SYMBOL_MAPPING[operator],
" and ".join(repr(operand) for operand in operands)),
details=details)
"""Unsupported operands."""
args = " and ".join(repr(operand) for operand in operands)
if operator in slots.COMPARES:
symbol = operator
details = f"Primitive types {args} are not comparable."
self.error(stack, f"unsupported operand types for {symbol}",
details=details)
else:
symbol = slots.SYMBOL_MAPPING[operator]
self.error(stack, f"unsupported operand type(s) for {symbol}: {args}",
details=details)

def invalid_annotation(self,
stack,
Expand Down
2 changes: 1 addition & 1 deletion pytype/pyc/magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def magic_word_to_version(magic_word):
as a string.
Returns:
A tuple (major, minor), e.g. (2, 7).
A tuple (major, minor), e.g. (3, 7).
"""
if not isinstance(magic_word, int):
magic_word = struct.unpack("<H", magic_word)[0]
Expand Down
2 changes: 1 addition & 1 deletion pytype/pytd/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def make_parser():
o.add_argument(
"-V", "--python_version", type=str, action="store",
dest="python_version", default="3",
help=("Python version to target (\"major.minor\", e.g. \"2.7\")"))
help=("Python version to target (\"major.minor\", e.g. \"3.7\")"))
o.add_argument(
"--multiline-args", action="store_true",
dest="multiline_args", default=False,
Expand Down
8 changes: 2 additions & 6 deletions pytype/pytd/pep484_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class TestPEP484(parser_test_base.ParserTest):
"""Test the visitors in optimize.py."""

def convert(self, t, python_version=None):
def convert(self, t):
"""Run ConvertTypingToNative and return the result as a string."""
return pytd_utils.Print(t.Visit(pep484.ConvertTypingToNative(None)))

Expand Down Expand Up @@ -36,11 +36,7 @@ def test_convert_any(self):

def test_convert_anystr(self):
t = pytd.NamedType("typing.AnyStr")
self.assertEqual(self.convert(t, python_version=(2, 7)),
"AnyStr")
t = pytd.NamedType("typing.AnyStr")
self.assertEqual(self.convert(t, python_version=(3, 5)),
"AnyStr")
self.assertEqual(self.convert(t), "AnyStr")


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion pytype/pytd/typeshed.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def get_typeshed_paths(self):
def get_pytd_paths(self):
"""Gets the paths to pytype's version-specific pytd files."""
return [pytype_source_utils.get_full_path(d)
for d in ("stubs/builtins/3", "stubs/stdlib/3")]
for d in ("stubs/builtins", "stubs/stdlib")]

def _list_modules(self, path, python_version):
"""Lists modules for _get_module_names_in_path."""
Expand Down
12 changes: 2 additions & 10 deletions pytype/pytd/typeshed_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,7 @@ def test_get_google_only_module_names(self):
modules = self.ts.get_all_module_names(self.python_version)
self.assertIn("pytypecanary", modules)

def test_get_all_module_names_2(self):
modules = self.ts.get_all_module_names((2, 7))
self.assertIn("collections", modules)
self.assertIn("csv", modules)
self.assertIn("ctypes", modules)
self.assertIn("xml.etree.ElementTree", modules)
self.assertIn("six.moves", modules)

def test_get_all_module_names_3(self):
def test_get_all_module_names(self):
modules = self.ts.get_all_module_names((3, 6))
self.assertIn("asyncio", modules)
self.assertIn("collections", modules)
Expand All @@ -71,7 +63,7 @@ def test_get_pytd_paths(self):
# reads from TYPESHED_HOME.

paths = {p.rsplit("pytype/", 1)[-1] for p in self.ts.get_pytd_paths()}
self.assertSetEqual(paths, {"stubs/builtins/3", "stubs/stdlib/3"})
self.assertSetEqual(paths, {"stubs/builtins", "stubs/stdlib"})
finally:
os.environ = old_env

Expand Down
1 change: 0 additions & 1 deletion pytype/stubs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ filegroup(
builtins/attr/setters.pytd
builtins/attr/validators.pytd
builtins/builtins.pytd
builtins/dataclasses.pytd
builtins/mypy_extensions.pytd
builtins/numpy/__init__.pytd
builtins/protocols.pytd
Expand Down
75 changes: 0 additions & 75 deletions pytype/stubs/builtins/dataclasses.pytd

This file was deleted.

Loading

0 comments on commit 9c96957

Please sign in to comment.