diff --git a/mypy/fastparse.py b/mypy/fastparse.py index 34fe2c0da32d..f3dd1f0a8fef 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -40,6 +40,7 @@ from mypy.errors import Errors from mypy.options import Options from mypy.reachability import mark_block_unreachable +from mypy.util import bytes_to_human_readable_repr try: # pull this into a final variable to make mypyc be quiet about the @@ -1638,17 +1639,3 @@ def stringify_name(n: AST) -> Optional[str]: if sv is not None: return "{}.{}".format(sv, n.attr) return None # Can't do it. - - -def bytes_to_human_readable_repr(b: bytes) -> str: - """Converts bytes into some human-readable representation. Unprintable - bytes such as the nul byte are escaped. For example: - - >>> b = bytes([102, 111, 111, 10, 0]) - >>> s = bytes_to_human_readable_repr(b) - >>> print(s) - foo\n\x00 - >>> print(repr(s)) - 'foo\\n\\x00' - """ - return repr(b)[2:-1] diff --git a/mypy/fastparse2.py b/mypy/fastparse2.py index 2d288bf158e5..bf3c09453ec0 100644 --- a/mypy/fastparse2.py +++ b/mypy/fastparse2.py @@ -47,10 +47,11 @@ from mypy import message_registry, errorcodes as codes from mypy.errors import Errors from mypy.fastparse import ( - TypeConverter, parse_type_comment, bytes_to_human_readable_repr, parse_type_ignore_tag, + TypeConverter, parse_type_comment, parse_type_ignore_tag, TYPE_IGNORE_PATTERN, INVALID_TYPE_IGNORE ) from mypy.options import Options +from mypy.util import bytes_to_human_readable_repr from mypy.reachability import mark_block_unreachable try: diff --git a/mypy/stubtest.py b/mypy/stubtest.py index 7228afaed446..0a05cf210409 100644 --- a/mypy/stubtest.py +++ b/mypy/stubtest.py @@ -25,7 +25,7 @@ from mypy import nodes from mypy.config_parser import parse_config_file from mypy.options import Options -from mypy.util import FancyFormatter +from mypy.util import FancyFormatter, bytes_to_human_readable_repr class Missing: @@ -942,6 +942,7 @@ def is_subtype_helper(left: mypy.types.Type, right: mypy.types.Type) -> bool: ): # Pretend Literal[0, 1] is a subtype of bool to avoid unhelpful errors. return True + with mypy.state.strict_optional_set(True): return mypy.subtypes.is_subtype(left, right) @@ -1029,17 +1030,19 @@ def anytype() -> mypy.types.AnyType: return mypy.types.TupleType(items, fallback) fallback = mypy.types.Instance(type_info, [anytype() for _ in type_info.type_vars]) - try: - # Literals are supposed to be only bool, int, str, bytes or enums, but this seems to work - # well (when not using mypyc, for which bytes and enums are also problematic). - return mypy.types.LiteralType( - value=runtime, - fallback=fallback, - ) - except TypeError: - # Ask for forgiveness if we're using mypyc. + + value: Union[bool, int, str] + if isinstance(runtime, bytes): + value = bytes_to_human_readable_repr(runtime) + elif isinstance(runtime, enum.Enum): + value = runtime.name + elif isinstance(runtime, (bool, int, str)): + value = runtime + else: return fallback + return mypy.types.LiteralType(value=value, fallback=fallback) + _all_stubs: Dict[str, nodes.MypyFile] = {} diff --git a/mypy/test/teststubtest.py b/mypy/test/teststubtest.py index e6eb8465c665..6cc556004337 100644 --- a/mypy/test/teststubtest.py +++ b/mypy/test/teststubtest.py @@ -750,6 +750,84 @@ def __init__(self, x): pass error="X.__init__" ) + @collect_cases + def test_good_literal(self) -> Iterator[Case]: + yield Case( + stub=r""" + from typing_extensions import Literal + + import enum + class Color(enum.Enum): + RED: int + + NUM: Literal[1] + CHAR: Literal['a'] + FLAG: Literal[True] + NON: Literal[None] + BYT1: Literal[b'abc'] + BYT2: Literal[b'\x90'] + ENUM: Literal[Color.RED] + """, + runtime=r""" + import enum + class Color(enum.Enum): + RED = 3 + + NUM = 1 + CHAR = 'a' + NON = None + FLAG = True + BYT1 = b"abc" + BYT2 = b'\x90' + ENUM = Color.RED + """, + error=None, + ) + + @collect_cases + def test_bad_literal(self) -> Iterator[Case]: + yield Case("from typing_extensions import Literal", "", None) # dummy case + yield Case( + stub="INT_FLOAT_MISMATCH: Literal[1]", + runtime="INT_FLOAT_MISMATCH = 1.0", + error="INT_FLOAT_MISMATCH", + ) + yield Case( + stub="WRONG_INT: Literal[1]", + runtime="WRONG_INT = 2", + error="WRONG_INT", + ) + yield Case( + stub="WRONG_STR: Literal['a']", + runtime="WRONG_STR = 'b'", + error="WRONG_STR", + ) + yield Case( + stub="BYTES_STR_MISMATCH: Literal[b'value']", + runtime="BYTES_STR_MISMATCH = 'value'", + error="BYTES_STR_MISMATCH", + ) + yield Case( + stub="STR_BYTES_MISMATCH: Literal['value']", + runtime="STR_BYTES_MISMATCH = b'value'", + error="STR_BYTES_MISMATCH", + ) + yield Case( + stub="WRONG_BYTES: Literal[b'abc']", + runtime="WRONG_BYTES = b'xyz'", + error="WRONG_BYTES", + ) + yield Case( + stub="WRONG_BOOL_1: Literal[True]", + runtime="WRONG_BOOL_1 = False", + error='WRONG_BOOL_1', + ) + yield Case( + stub="WRONG_BOOL_2: Literal[False]", + runtime="WRONG_BOOL_2 = True", + error='WRONG_BOOL_2', + ) + def remove_color_code(s: str) -> str: return re.sub("\\x1b.*?m", "", s) # this works! diff --git a/mypy/util.py b/mypy/util.py index 533e9c6bee6e..9f620e823e91 100644 --- a/mypy/util.py +++ b/mypy/util.py @@ -105,6 +105,20 @@ def find_python_encoding(text: bytes, pyversion: Tuple[int, int]) -> Tuple[str, return default_encoding, -1 +def bytes_to_human_readable_repr(b: bytes) -> str: + """Converts bytes into some human-readable representation. Unprintable + bytes such as the nul byte are escaped. For example: + + >>> b = bytes([102, 111, 111, 10, 0]) + >>> s = bytes_to_human_readable_repr(b) + >>> print(s) + foo\n\x00 + >>> print(repr(s)) + 'foo\\n\\x00' + """ + return repr(b)[2:-1] + + class DecodeError(Exception): """Exception raised when a file cannot be decoded due to an unknown encoding type.