From cb7949b14e7e4c2200f8398e58795960bf829c3a Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sat, 18 Mar 2017 14:50:31 -0700 Subject: [PATCH] fixes to dis stubs (#1025) - Add sys.version_info checks (mypy now supports them) - Went over Python 3 docs and corrected a few things - Reexport things that are imported from opcode --- stdlib/2and3/opcode.pyi | 4 +- stdlib/3/dis.pyi | 86 +++++++++++++++++++++-------------------- 2 files changed, 46 insertions(+), 44 deletions(-) diff --git a/stdlib/2and3/opcode.pyi b/stdlib/2and3/opcode.pyi index a027963d3..4e72eae7a 100644 --- a/stdlib/2and3/opcode.pyi +++ b/stdlib/2and3/opcode.pyi @@ -1,4 +1,4 @@ -from typing import List, Dict, Sequence +from typing import List, Dict, Optional, Sequence import sys @@ -17,7 +17,7 @@ HAVE_ARGUMENT = ... # type: int EXTENDED_ARG = ... # type: int if sys.version_info >= (3, 4): - def stack_effect(opcode: int, oparg: int = ...) -> int: ... + def stack_effect(opcode: int, oparg: Optional[int] = ...) -> int: ... if sys.version_info >= (3, 6): hasnargs = ... # type: List[int] diff --git a/stdlib/3/dis.pyi b/stdlib/3/dis.pyi index a13443a5c..3d693276b 100644 --- a/stdlib/3/dis.pyi +++ b/stdlib/3/dis.pyi @@ -3,48 +3,48 @@ from typing import List, Union, Iterator, Tuple, Optional, Any, IO, NamedTuple, import sys import types -from opcode import (hasconst, hasname, hasjrel, hasjabs, haslocal, hascompare, - hasfree, cmp_op, opname, opmap, HAVE_ARGUMENT, - EXTENDED_ARG) +from opcode import (hasconst as hasconst, hasname as hasname, hasjrel as hasjrel, + hasjabs as hasjabs, haslocal as haslocal, hascompare as hascompare, + hasfree as hasfree, cmp_op as cmp_op, opname as opname, opmap as opmap, + HAVE_ARGUMENT as HAVE_ARGUMENT, EXTENDED_ARG as EXTENDED_ARG) if sys.version_info >= (3, 4): - from opcode import stack_effect + from opcode import stack_effect as stack_effect if sys.version_info >= (3, 6): - from opcode import hasnargs + from opcode import hasnargs as hasnargs _have_code = Union[types.MethodType, types.FunctionType, types.CodeType, type] _have_code_or_string = Union[_have_code, str, bytes] -Instruction = NamedTuple( - "Instruction", - [ - ('opname', str), - ('opcode', int), - ('arg', Optional[int]), - ('argval', Any), - ('argrepr', str), - ('offset', int), - ('starts_line', Optional[int]), - ('is_jump_target', bool) - ] -) +if sys.version_info >= (3, 4): + Instruction = NamedTuple( + "Instruction", + [ + ('opname', str), + ('opcode', int), + ('arg', Optional[int]), + ('argval', Any), + ('argrepr', str), + ('offset', int), + ('starts_line', Optional[int]), + ('is_jump_target', bool) + ] + ) + class Bytecode: + codeobj = ... # type: types.CodeType + first_line = ... # type: int + def __init__(self, x: _have_code_or_string, *, first_line: Optional[int] = ..., + current_offset: Optional[int] = ...) -> None: ... + def __iter__(self) -> Iterator[Instruction]: ... + def __repr__(self) -> str: ... + def info(self) -> str: ... + def dis(self) -> str: ... -# if sys.version_info >= (3, 4): -class Bytecode: - codeobj = ... # type: types.CodeType - first_line = ... # type: int - def __init__(self, x: _have_code_or_string, *, first_line: int=..., - current_offset: int=...) -> None: ... - def __iter__(self) -> Iterator[Instruction]: ... - def __repr__(self) -> str: ... - def info(self) -> str: ... - def dis(self) -> str: ... - - @classmethod - def from_traceback(cls, tb: types.TracebackType) -> Bytecode: ... + @classmethod + def from_traceback(cls, tb: types.TracebackType) -> Bytecode: ... COMPILER_FLAG_NAMES = ... # type: Dict[int, str] @@ -54,17 +54,19 @@ def pretty_flags(flags: int) -> str: ... def findlabels(code: _have_code) -> List[int]: ... def findlinestarts(code: _have_code) -> Iterator[Tuple[int, int]]: ... -# Signature changes are not allowed by mypy -# 'All conditional function variants must have identical signatures' -# TODO: mypy issue #698 - -# if sys.version_info >= (3, 2): def code_info(x: _have_code_or_string) -> str: ... -# `file` parameter requires sys.version_info >= (3, 4): -def dis(x: _have_code_or_string = ..., *, file: IO[str] = None) -> None: ... -def distb(tb: types.TracebackType = ..., *, file: IO[str] = None) -> None: ... -def disassemble(co: _have_code, lasti: int = ..., *, file: IO[str] = None) -> None: ... -def show_code(co: _have_code, *, file: IO[str] = None) -> None: ... +if sys.version_info >= (3, 4): + def dis(x: _have_code_or_string = ..., *, file: Optional[IO[str]] = ...) -> None: ... + def distb(tb: Optional[types.TracebackType] = ..., *, file: Optional[IO[str]] = ...) -> None: ... + def disassemble(co: _have_code, lasti: int = ..., *, file: Optional[IO[str]] = ...) -> None: ... + def disco(co: _have_code, lasti: int = ..., *, file: Optional[IO[str]] = ...) -> None: ... + def show_code(co: _have_code, *, file: Optional[IO[str]] = ...) -> None: ... -def get_instructions(x: _have_code, *, first_line: int = ...) -> Iterator[Instruction]: ... + def get_instructions(x: _have_code, *, first_line: Optional[int] = ...) -> Iterator[Instruction]: ... +else: + def dis(x: _have_code_or_string = ...) -> None: ... + def distb(tb: types.TracebackType = ...) -> None: ... + def disassemble(co: _have_code, lasti: int = ...) -> None: ... + def disco(co: _have_code, lasti: int = ...) -> None: ... + def show_code(co: _have_code) -> None: ...