Skip to content

Commit

Permalink
Merge branch 'typing-from-typehinting'
Browse files Browse the repository at this point in the history
Switch to using Guido's typing.py from
https://github.com/ambv/typehinting/tree/master/prototyping.
This only affects Python 3 right now.

Fixes #530.
  • Loading branch information
JukkaL committed Mar 26, 2015
2 parents 4541077 + 491a7db commit 91b6021
Show file tree
Hide file tree
Showing 23 changed files with 2,572 additions and 712 deletions.
7 changes: 4 additions & 3 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Mypy is licensed under the terms of the MIT license, reproduced below.

The MIT License

Copyright (c) 2012 Jukka Lehtosalo and contributors
Copyright (c) 2015 Jukka Lehtosalo and contributors

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
Expand All @@ -26,8 +26,9 @@ DEALINGS IN THE SOFTWARE.

= = = = =

Portions of mypy are licensed under different licenses. The files under
python-lib are licensed under the PSF 2 License, reproduced below.
Portions of mypy are licensed under different licenses. The files
under lib-python and lib-typing/3.2 are licensed under the PSF 2
License, reproduced below.

= = = = =

Expand Down
4 changes: 2 additions & 2 deletions lib-python/3.2/pprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def _format(self, object: object, stream: TextIO, indent: int,
(issubclass(typ, set) and r is set.__repr__) or
(issubclass(typ, frozenset) and r is frozenset.__repr__)
):
anyobj = Any(object) # TODO Collection?
anyobj = cast(Any, object) # TODO Collection?
length = _len(anyobj)
if issubclass(typ, list):
write('[')
Expand Down Expand Up @@ -322,7 +322,7 @@ def _safe_repr(object: object, context: Dict[int, int],

if (issubclass(typ, list) and r is list.__repr__) or \
(issubclass(typ, tuple) and r is tuple.__repr__):
anyobj = Any(object) # TODO Sequence?
anyobj = cast(Any, object) # TODO Sequence?
if issubclass(typ, list):
if not object:
return "[]", True, False
Expand Down
14 changes: 7 additions & 7 deletions lib-python/3.2/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,9 @@ def __str__(self) -> str:
import _subprocess
class STARTUPINFO:
dwFlags = 0
hStdInput = Any(None)
hStdOutput = Any(None)
hStdError = Any(None)
hStdInput = cast(Any, None)
hStdOutput = cast(Any, None)
hStdError = cast(Any, None)
wShowWindow = 0
class pywintypes:
error = IOError
Expand Down Expand Up @@ -1048,8 +1048,8 @@ def _readerthread(self, fh: IO[AnyStr], buffer: List[AnyStr]) -> None:


def _communicate(self, input: Any) -> Tuple[Any, Any]:
stdout = Any(None) # Return
stderr = Any(None) # Return
stdout = cast(Any, None) # Return
stderr = cast(Any, None) # Return

if self.stdout:
stdout = []
Expand Down Expand Up @@ -1483,8 +1483,8 @@ def _communicate(self, input: Any) -> Tuple[Any, Any]:

# Translate newlines, if requested.
# This also turns bytes into strings.
stdout3 = Any(stdout2)
stderr3 = Any(stderr2)
stdout3 = cast(Any, stdout2)
stderr3 = cast(Any, stderr2)
if self.universal_newlines:
if stdout is not None:
stdout3 = self._translate_newlines(
Expand Down
2 changes: 1 addition & 1 deletion lib-python/3.2/tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ def __getattr__(self, name: str) -> _Any:
# Attribute lookups are delegated to the underlying file
# and cached for non-numeric results
# (i.e. methods are cached, closed and friends are not)
file = _Any(self).__dict__['file'] # type: _IO[_Any]
file = _cast(_Any, self).__dict__['file'] # type: _IO[_Any]
a = getattr(file, name)
if not isinstance(a, int):
setattr(self, name, a)
Expand Down
4 changes: 2 additions & 2 deletions lib-python/3.2/test/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import logging.handlers

import _thread, threading
from typing import Any, Dict
from typing import Any, Dict, cast
#try:
# import multiprocessing.process
#except ImportError:
Expand Down Expand Up @@ -441,7 +441,7 @@ def fcmp(x, y): # fuzzy comparison function

# decorator for skipping tests on non-IEEE 754 platforms
requires_IEEE_754 = unittest.skipUnless(
(Any(float)).__getformat__("double").startswith("IEEE"),
cast(Any, float).__getformat__("double").startswith("IEEE"),
"test requires IEEE 754 doubles")

is_jython = sys.platform.startswith('java')
Expand Down
4 changes: 2 additions & 2 deletions lib-python/3.2/test/test_getopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import getopt

from typing import Any
from typing import cast, Any

sentinel = object()

Expand All @@ -22,7 +22,7 @@ def tearDown(self) -> None:

def assertError(self, *args: Any, **kwargs: Any) -> None:
# JLe: work around mypy bug #229
Any(self.assertRaises)(getopt.GetoptError, *args, **kwargs)
cast(Any, self.assertRaises)(getopt.GetoptError, *args, **kwargs)

def test_short_has_arg(self) -> None:
self.assertTrue(getopt.short_has_arg('a', 'a:'))
Expand Down
4 changes: 2 additions & 2 deletions lib-python/3.2/test/test_posixpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from posixpath import realpath, abspath, dirname, basename

import posix
from typing import Any, TypeVar, Callable
from typing import cast, Any, TypeVar, Callable

T = TypeVar('T')

Expand Down Expand Up @@ -225,7 +225,7 @@ def test_samestat_on_links(self) -> None:
test_fn2 = support.TESTFN + "2"
self._create_file(test_fn1)
test_fns = [test_fn1, test_fn2]
Any(os.symlink)(*test_fns)
cast(Any, os.symlink)(*test_fns)
stats = map(os.stat, test_fns)
self.assertTrue(posixpath.samestat(*stats))
os.remove(test_fn2)
Expand Down
5 changes: 3 additions & 2 deletions lib-python/3.2/test/test_pprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ def test_same_as_repr(self) -> None:
-6, -6, complex(-6.,-6.), -1.5, "x", b"x", (3,), [3], {3: 6},
(1,2), [3,4], {5: 6},
tuple2((1,2)), tuple3((1,2)), tuple3(range(100)),
[3,4], list2(Any([3,4])), list3(Any([3,4])), list3(Any(range(100))),
dict2(Any({5: 6})), dict3(Any({5: 6})), # JLe: work around mypy issue #233
[3,4], list2(cast(Any, [3,4])), list3(cast(Any, [3,4])),
list3(cast(Any, range(100))), dict2(cast(Any, {5: 6})),
dict3(cast(Any, {5: 6})), # JLe: work around mypy issue #233
range(10, -11, -1)
):
native = repr(simple)
Expand Down
4 changes: 2 additions & 2 deletions lib-python/3.2/test/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from math import log, exp, pi, fsum, sin
from test import support

from typing import Undefined, Any, Dict, List, Callable, Generic, TypeVar
from typing import Undefined, Any, Dict, List, Callable, Generic, TypeVar, cast

RT = TypeVar('RT', random.Random, random.SystemRandom)

Expand Down Expand Up @@ -267,7 +267,7 @@ def test_setstate_middle_arg(self) -> None:
# Wrong type, s/b tuple of 625 ints
self.assertRaises(TypeError, self.gen.setstate, (2, tuple(['a',]*625), None))
# Last element s/b an int also
self.assertRaises(TypeError, self.gen.setstate, (2, Any((0,))*624+('a',), None))
self.assertRaises(TypeError, self.gen.setstate, (2, cast(Any, (0,))*624+('a',), None))

def test_referenceImplementation(self) -> None:
# Compare the python implementation with results from the original
Expand Down
4 changes: 2 additions & 2 deletions lib-python/3.2/test/test_shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ def mock_rename(func: Any) -> Any:
def wrap(*args: Any, **kwargs: Any) -> Any:
try:
builtin_rename = shutil.rename
shutil.rename = Any(_fake_rename)
shutil.rename = cast(Any, _fake_rename)
return func(*args, **kwargs)
finally:
shutil.rename = Any(builtin_rename)
shutil.rename = cast(Any, builtin_rename)
return wrap

class TestShutil(unittest.TestCase):
Expand Down
14 changes: 7 additions & 7 deletions lib-python/3.2/test/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import resource

from typing import Any, Dict, Callable, Iterable, List, Set, Tuple
from typing import Any, Dict, Callable, Iterable, List, Set, Tuple, cast

mswindows = (sys.platform == "win32")

Expand All @@ -40,7 +40,7 @@ def _mkstemp() -> Tuple[int, str]:
"""Replacement for mkstemp, calling mktemp."""
fname = tempfile.mktemp()
return os.open(fname, os.O_RDWR|os.O_CREAT), fname
mkstemp = Any(_mkstemp)
mkstemp = cast(Any, _mkstemp)


class BaseTestCase(unittest.TestCase):
Expand Down Expand Up @@ -89,7 +89,7 @@ def test_check_output(self) -> None:
# check_output() function with zero return code
output = subprocess.check_output(
[sys.executable, "-c", "print('BDFL')"])
self.assertIn(b'BDFL', Any(output)) # see #39
self.assertIn(b'BDFL', cast(Any, output)) # see #39

def test_check_output_nonzero(self) -> None:
# check_call() function with non-zero return code
Expand All @@ -103,7 +103,7 @@ def test_check_output_stderr(self) -> None:
output = subprocess.check_output(
[sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"],
stderr=subprocess.STDOUT)
self.assertIn(b'BDFL', Any(output)) # see #39
self.assertIn(b'BDFL', cast(Any, output)) # see #39

def test_check_output_stdout_arg(self) -> None:
# check_output() function stderr redirected to stdout
Expand Down Expand Up @@ -633,7 +633,7 @@ def test_invalid_bufsize(self) -> None:
# an invalid type of the bufsize argument should raise
# TypeError.
with self.assertRaises(TypeError):
subprocess.Popen([sys.executable, "-c", "pass"], Any("orange"))
subprocess.Popen([sys.executable, "-c", "pass"], cast(Any, "orange"))

def test_bufsize_is_none(self) -> None:
# bufsize=None should be the same as bufsize=0.
Expand Down Expand Up @@ -897,7 +897,7 @@ def test_args_string(self) -> None:
# args is a string
fd, fname = mkstemp()
# reopen in text mode
with open(fd, "w", errors=Any("surrogateescape")) as fobj: # see #260
with open(fd, "w", errors=cast(Any, "surrogateescape")) as fobj: # see #260
fobj.write("#!/bin/sh\n")
fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
sys.executable)
Expand Down Expand Up @@ -942,7 +942,7 @@ def test_call_string(self) -> None:
# call() function with string argument on UNIX
fd, fname = mkstemp()
# reopen in text mode
with open(fd, "w", errors=Any("surrogateescape")) as fobj: # see #260
with open(fd, "w", errors=cast(Any, "surrogateescape")) as fobj: # see #260
fobj.write("#!/bin/sh\n")
fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
sys.executable)
Expand Down
Loading

0 comments on commit 91b6021

Please sign in to comment.