From faba48058a49c78bfb594b30342cf5042bebf235 Mon Sep 17 00:00:00 2001 From: Elazar Gershuni Date: Mon, 4 Jul 2016 14:02:37 +0300 Subject: [PATCH 1/4] Add stubs for dis and opcode --- stdlib/3/dis.pyi | 70 +++++++++++++++++++++++++++++++++++++++++++++ stdlib/3/opcode.pyi | 20 +++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 stdlib/3/dis.pyi create mode 100644 stdlib/3/opcode.pyi diff --git a/stdlib/3/dis.pyi b/stdlib/3/dis.pyi new file mode 100644 index 000000000000..294d1901d36d --- /dev/null +++ b/stdlib/3/dis.pyi @@ -0,0 +1,70 @@ +from typing import Any, List, Union, NamedTuple, Iterator, Tuple, IO, Optional, Dict + +from opcode import cmp_op,hasconst ,hasname , hasjrel, hasjabs, haslocal, hascompare, hasfree, opname ,opmap ,HAVE_ARGUMENT, EXTENDED_ARG, hasnargs + +import types + +_have_code = Union[types.MethodType, types.FunctionType, types.CodeType, type] +_have_code_or_string = Union[str, bytes, types.MethodType, types.FunctionType, types.CodeType, type] + +class Instruction(NamedTuple("Instruction", [ + ('opname', str), + ('opcode', int), + ('arg', Optional[int]), + ('argval', Any), + ('argrepr', str), + ('offset', int), + ('starts_line', Optional[int]), + ('is_jump_target', bool) + ])): + # ad-hoc - seems to be an error in the NamedTuple type hint + def _replace(self, **kwargs:Any) -> Instruction: ... + def _fields(self) -> List[str]: ... + + +import sys +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: ... + +COMPILER_FLAG_NAMES = ... # type: Dict[int, str] + + +def pretty_flags(flags:int) -> str: ... +def findlabels(code:_have_code) -> List[int]: ... +def findlinestarts(code:_have_code) -> Iterator[Tuple[int, int]]: ... + +if sys.version_info < (3, 4): + # signature changes are not allowed by mypy + #def dis(x:_have_code_or_string=...) -> None: ... + #def distb(tb:types.TracebackType=...) -> None: ... + #def disassemble(co:_have_code, lasti:int=...) -> None: ... + def dis(x:_have_code_or_string=..., *, file=...) -> None: ... # type: ignore + def distb(tb:types.TracebackType=..., *, file:IO[str]=...) -> None: ... # type: ignore + def disassemble(co:_have_code, lasti:int=..., *, file=None) -> None: ... # type: ignore + +if (3, 2) <= sys.version_info: + def code_info(x:_have_code_or_string) -> str: ... + +if (3, 2) <= sys.version_info < (3, 4): + #def show_code(co:_have_code, *, file:IO[str]=...) -> None: ... + def show_code(co:_have_code, *, file:IO[str]=...) -> None: ... # type: ignore + +if sys.version_info >= (3, 4): + def dis(x:_have_code_or_string=..., *, file=...) -> None: ... + def distb(tb:types.TracebackType=..., *, file:IO[str]=...) -> None: ... + def disassemble(co:_have_code, lasti:int=..., *, file=None) -> None: ... + + def show_code(co:_have_code, *, file:IO[str]=...) -> None: ... + + def get_instructions(x:_have_code, *, first_line:int=...) -> Iterator[Instruction]: ... + def stack_effect(opcode: int, oparg:int=...) -> int: ... diff --git a/stdlib/3/opcode.pyi b/stdlib/3/opcode.pyi new file mode 100644 index 000000000000..7c39233b3224 --- /dev/null +++ b/stdlib/3/opcode.pyi @@ -0,0 +1,20 @@ +from typing import List, Dict, Tuple + +cmp_op = ... # type: Tuple[str,str,str,str,str,str,str,str,str,str,str,str] +hasconst = ... # type: List[int] +hasname = ... # type: List[int] +hasjrel = ... # type: List[int] +hasjabs = ... # type: List[int] +haslocal = ... # type: List[int] +hascompare = ... # type: List[int] +hasfree = ... # type: List[int] +opname = ... # type: List[str] + +opmap = ... # Dict[str, int] +HAVE_ARGUMENT = ... # type: int +EXTENDED_ARG = ... # type: int +hasnargs = ... # type: List[int] + +import sys +if sys.version_info >= (3, 4): + def stack_effect(opcode: int, oparg:int=...) -> int: ... From 8d44dd7c47477c2b33de832e89daaaa876100e1a Mon Sep 17 00:00:00 2001 From: Elazar Gershuni Date: Tue, 5 Jul 2016 17:22:18 +0300 Subject: [PATCH 2/4] pep8-ing and precision --- stdlib/3/dis.pyi | 92 +++++++++++++++++++++++---------------------- stdlib/3/opcode.pyi | 32 ++++++++-------- 2 files changed, 62 insertions(+), 62 deletions(-) diff --git a/stdlib/3/dis.pyi b/stdlib/3/dis.pyi index 294d1901d36d..9c82abdea0ca 100644 --- a/stdlib/3/dis.pyi +++ b/stdlib/3/dis.pyi @@ -1,11 +1,15 @@ -from typing import Any, List, Union, NamedTuple, Iterator, Tuple, IO, Optional, Dict +from typing import (List, Union, Iterator, Iterable, Tuple, Optional, Dict, + Any, IO, NamedTuple) -from opcode import cmp_op,hasconst ,hasname , hasjrel, hasjabs, haslocal, hascompare, hasfree, opname ,opmap ,HAVE_ARGUMENT, EXTENDED_ARG, hasnargs +from opcode import (hasconst, hasname, hasjrel, hasjabs, haslocal, hascompare, + hasfree, hasnargs, cmp_op, opname , opmap , HAVE_ARGUMENT, + EXTENDED_ARG, stack_effect) import types _have_code = Union[types.MethodType, types.FunctionType, types.CodeType, type] -_have_code_or_string = Union[str, bytes, types.MethodType, types.FunctionType, types.CodeType, type] +_have_code_or_string = Union[_have_code, str, bytes] + class Instruction(NamedTuple("Instruction", [ ('opname', str), @@ -16,55 +20,53 @@ class Instruction(NamedTuple("Instruction", [ ('offset', int), ('starts_line', Optional[int]), ('is_jump_target', bool) - ])): + ])): # ad-hoc - seems to be an error in the NamedTuple type hint - def _replace(self, **kwargs:Any) -> Instruction: ... - def _fields(self) -> List[str]: ... + # TODO: mypy issue #1076 + _fields = ... # type: List[str] + _source = ... # type: str + def _replace(self, *, opname: str = ..., opcode: int = ..., + arg: Optional[int] = ..., argval: Any = ..., argrepr: str = ..., + offset: int = ..., starts_line: Optional[int] = ..., + is_jump_target: bool = ...) -> Instruction: ... + def _asdict(self) -> Dict[str, Any]: ... + @staticmethod + def _make(iterable: Iterable[Any]) -> Instruction: ... -import sys -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: ... - -COMPILER_FLAG_NAMES = ... # type: Dict[int, 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: ... -def pretty_flags(flags:int) -> str: ... -def findlabels(code:_have_code) -> List[int]: ... -def findlinestarts(code:_have_code) -> Iterator[Tuple[int, int]]: ... + +COMPILER_FLAG_NAMES = ... # type: Dict[int, str] -if sys.version_info < (3, 4): - # signature changes are not allowed by mypy - #def dis(x:_have_code_or_string=...) -> None: ... - #def distb(tb:types.TracebackType=...) -> None: ... - #def disassemble(co:_have_code, lasti:int=...) -> None: ... - def dis(x:_have_code_or_string=..., *, file=...) -> None: ... # type: ignore - def distb(tb:types.TracebackType=..., *, file:IO[str]=...) -> None: ... # type: ignore - def disassemble(co:_have_code, lasti:int=..., *, file=None) -> None: ... # type: ignore -if (3, 2) <= sys.version_info: - def code_info(x:_have_code_or_string) -> str: ... - -if (3, 2) <= sys.version_info < (3, 4): - #def show_code(co:_have_code, *, file:IO[str]=...) -> None: ... - def show_code(co:_have_code, *, file:IO[str]=...) -> None: ... # type: ignore +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 #1801 -if sys.version_info >= (3, 4): - def dis(x:_have_code_or_string=..., *, file=...) -> None: ... - def distb(tb:types.TracebackType=..., *, file:IO[str]=...) -> None: ... - def disassemble(co:_have_code, lasti:int=..., *, file=None) -> None: ... +# if sys.version_info >= (3, 2): +def code_info(x: _have_code_or_string) -> str: ... - def show_code(co:_have_code, *, file:IO[str]=...) -> None: ... +# `file` parameter requires sys.version_info >= (3, 4): +def dis(x: _have_code_or_string = ..., *, file = ...) -> None: ... +def distb(tb: types.TracebackType = ..., *, file: IO[str] = ...) -> None: ... +def disassemble(co: _have_code, lasti: int = ..., *, file = ...) -> None: ... +def show_code(co: _have_code, *, file: IO[str]=...) -> None: ... - def get_instructions(x:_have_code, *, first_line:int=...) -> Iterator[Instruction]: ... - def stack_effect(opcode: int, oparg:int=...) -> int: ... +def get_instructions(x: _have_code, *, first_line: int = ...) -> Iterator[Instruction]: ... diff --git a/stdlib/3/opcode.pyi b/stdlib/3/opcode.pyi index 7c39233b3224..e577a9ff5795 100644 --- a/stdlib/3/opcode.pyi +++ b/stdlib/3/opcode.pyi @@ -1,20 +1,18 @@ -from typing import List, Dict, Tuple +from typing import List, Dict, Sequence -cmp_op = ... # type: Tuple[str,str,str,str,str,str,str,str,str,str,str,str] -hasconst = ... # type: List[int] -hasname = ... # type: List[int] -hasjrel = ... # type: List[int] -hasjabs = ... # type: List[int] -haslocal = ... # type: List[int] -hascompare = ... # type: List[int] -hasfree = ... # type: List[int] -opname = ... # type: List[str] +cmp_op = ... # type: Sequence[str] +hasconst = ... # type: List[int] +hasname = ... # type: List[int] +hasjrel = ... # type: List[int] +hasjabs = ... # type: List[int] +haslocal = ... # type: List[int] +hascompare = ... # type: List[int] +hasfree = ... # type: List[int] +opname = ... # type: List[str] -opmap = ... # Dict[str, int] -HAVE_ARGUMENT = ... # type: int -EXTENDED_ARG = ... # type: int -hasnargs = ... # type: List[int] +opmap = ... # Dict[str, int] +HAVE_ARGUMENT = ... # type: int +EXTENDED_ARG = ... # type: int +hasnargs = ... # type: List[int] -import sys -if sys.version_info >= (3, 4): - def stack_effect(opcode: int, oparg:int=...) -> int: ... +def stack_effect(opcode: int, oparg: int = ...) -> int: ... From 695aa773ce676f27c9477015846e2a2b1ab8bf6b Mon Sep 17 00:00:00 2001 From: Elazar Gershuni Date: Tue, 5 Jul 2016 19:57:58 +0300 Subject: [PATCH 3/4] fix issue ref, trailing space --- stdlib/3/dis.pyi | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/stdlib/3/dis.pyi b/stdlib/3/dis.pyi index 9c82abdea0ca..8c08946ad815 100644 --- a/stdlib/3/dis.pyi +++ b/stdlib/3/dis.pyi @@ -1,5 +1,5 @@ from typing import (List, Union, Iterator, Iterable, Tuple, Optional, Dict, - Any, IO, NamedTuple) + Any, IO, NamedTuple) from opcode import (hasconst, hasname, hasjrel, hasjabs, haslocal, hascompare, hasfree, hasnargs, cmp_op, opname , opmap , HAVE_ARGUMENT, @@ -20,7 +20,7 @@ class Instruction(NamedTuple("Instruction", [ ('offset', int), ('starts_line', Optional[int]), ('is_jump_target', bool) - ])): + ])): # ad-hoc - seems to be an error in the NamedTuple type hint # TODO: mypy issue #1076 _fields = ... # type: List[str] @@ -35,7 +35,7 @@ class Instruction(NamedTuple("Instruction", [ # if sys.version_info >= (3, 4): -class Bytecode: +class Bytecode: codeobj = ... # type: types.CodeType first_line = ... # type: int def __init__(self, x: _have_code_or_string, *, first_line: int=..., @@ -58,12 +58,12 @@ 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 #1801 +# TODO: mypy issue #698 -# if sys.version_info >= (3, 2): +# if sys.version_info >= (3, 2): def code_info(x: _have_code_or_string) -> str: ... -# `file` parameter requires sys.version_info >= (3, 4): +# `file` parameter requires sys.version_info >= (3, 4): def dis(x: _have_code_or_string = ..., *, file = ...) -> None: ... def distb(tb: types.TracebackType = ..., *, file: IO[str] = ...) -> None: ... def disassemble(co: _have_code, lasti: int = ..., *, file = ...) -> None: ... From fba6186c3375e61113c218b5945c5bd08c73c362 Mon Sep 17 00:00:00 2001 From: Elazar Gershuni Date: Wed, 17 Aug 2016 02:44:10 +0300 Subject: [PATCH 4/4] remove NamedTuple workaround --- stdlib/3/dis.pyi | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/stdlib/3/dis.pyi b/stdlib/3/dis.pyi index 8c08946ad815..47776148a6bb 100644 --- a/stdlib/3/dis.pyi +++ b/stdlib/3/dis.pyi @@ -1,5 +1,4 @@ -from typing import (List, Union, Iterator, Iterable, Tuple, Optional, Dict, - Any, IO, NamedTuple) +from typing import List, Union, Iterator, Tuple, Optional, Any, IO, NamedTuple from opcode import (hasconst, hasname, hasjrel, hasjabs, haslocal, hascompare, hasfree, hasnargs, cmp_op, opname , opmap , HAVE_ARGUMENT, @@ -11,7 +10,7 @@ _have_code = Union[types.MethodType, types.FunctionType, types.CodeType, type] _have_code_or_string = Union[_have_code, str, bytes] -class Instruction(NamedTuple("Instruction", [ +Instruction = NamedTuple("Instruction", [ ('opname', str), ('opcode', int), ('arg', Optional[int]), @@ -20,18 +19,7 @@ class Instruction(NamedTuple("Instruction", [ ('offset', int), ('starts_line', Optional[int]), ('is_jump_target', bool) - ])): - # ad-hoc - seems to be an error in the NamedTuple type hint - # TODO: mypy issue #1076 - _fields = ... # type: List[str] - _source = ... # type: str - def _replace(self, *, opname: str = ..., opcode: int = ..., - arg: Optional[int] = ..., argval: Any = ..., argrepr: str = ..., - offset: int = ..., starts_line: Optional[int] = ..., - is_jump_target: bool = ...) -> Instruction: ... - def _asdict(self) -> Dict[str, Any]: ... - @staticmethod - def _make(iterable: Iterable[Any]) -> Instruction: ... + ]) # if sys.version_info >= (3, 4):