Skip to content

Typeshed #884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Oct 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "typeshed"]
path = typeshed
url = http://github.com/python/typeshed
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Quick start for contributing to mypy

If you want to contribute, first clone the mypy git repository:

$ git clone https://github.com/JukkaL/mypy.git
$ git clone --recurse-submodules https://github.com/JukkaL/mypy.git

Run the supplied `setup.py` script to install mypy:

Expand All @@ -130,6 +130,19 @@ The mypy wiki contains some useful information for contributors:

http://www.mypy-lang.org/wiki/DeveloperGuides

Working with the git version of mypy
------------------------------------

mypy contains a submodule, "typeshed". See http://github.com/python/typeshed.
This submodule contains types for the Python standard library.

Due to the way git submodules work, you'll have to do
```
git submodule update typeshed
```
whenever you change branches, merge, rebase, or pull.

(It's possible to automate this: Search Google for "git hook update submodule")

Running tests and linting
-------------------------
Expand Down
19 changes: 12 additions & 7 deletions docs/source/basics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ explains how to download and install mypy.

.. _library-stubs:

Library stubs
*************
Typeshed
********

In order to type check code that uses library modules such as those
included in the Python standard library, you need to have library
Expand All @@ -141,8 +141,9 @@ For example, consider this code:

Without a library stub, the type checker has no way of inferring the
type of ``x`` and checking that the argument to ``chr`` has a valid
type. Mypy comes with a library stub for Python builtins that contains
a definition like this for ``chr``:
type. Mypy contains the `typeshed <http://github.com/python/typeshed>`_ project,
which contains library stubs for Python builtins that contains a definition
like this for ``chr``:

.. code-block:: python

Expand All @@ -167,9 +168,13 @@ for module ``csv``, and use a subdirectory with ``__init__.pyi`` for packages.
If there is both a ``.py`` and a ``.pyi`` file for a module, the ``.pyi`` file
takes precedence. This way you can easily add annotations for a module even if
you don't want to modify the source code. This can be useful, for example, if you
use 3rd party open source libraries in your program. You can also override the stubs
mypy uses for standard libary modules, in case you need to make local
modifications.
use 3rd party open source libraries in your program.

You can also override the stubs mypy uses for standard libary modules, in case
you need to make local modifications. (Note that if you want to submit your
changes, please submit a pull request to `typeshed <http://github.com/python/typeshed>`_
first, and then update the submodule in mypy using a commit that only touches
the typeshed submodule and nothing else)

That's it! Now you can access the module in mypy programs and type check
code that uses the library. If you write a stub for a library module,
Expand Down
43 changes: 14 additions & 29 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,43 +212,28 @@ def default_lib_path(data_dir: str, target: int, pyversion: Tuple[int, int],
if path_env is not None:
path[:0] = path_env.split(os.pathsep)

# Add library stubs directory. By convention, they are stored in the
# stubs/x.y directory of the mypy installation. Additionally, stubs
# for earlier versions in the same major version will be added, and
# as a last resort, third-party stubs will be added.
if pyversion == 2:
major, minor = 2, 7
else:
# See bug #886
major, minor = sys.version_info[0], sys.version_info[1]
version_dir = '3.2'
third_party_dir = 'third-party-3.2'
if pyversion[0] < 3:
version_dir = '2.7'
third_party_dir = 'third-party-2.7'
path.append(os.path.join(data_dir, 'stubs', version_dir))
path.append(os.path.join(data_dir, 'stubs', third_party_dir))
path.append(os.path.join(data_dir, 'stubs-auto', version_dir))
if major == 3:
# Add additional stub directories.
versions = ['3.3', '3.4', '3.5', '3.6']
if False:
# Ick, we really should figure out how to use this again.
versions = ['3.%d' % i for i in range(minor, -1, -1)]
for v in versions:
stubdir = os.path.join(data_dir, 'stubs', v)
auto = os.path.join(data_dir, 'stubs-auto')
if os.path.isdir(auto):
data_dir = auto

# We allow a module for e.g. version 3.5 to be in 3.4/. The assumption
# is that a module added with 3.4 will still be present in Python 3.5.
versions = ["%d.%d" % (pyversion[0], minor)
for minor in reversed(range(pyversion[1] + 1))]
# E.g. for Python 3.5, try 2and3/, then 3/, then 3.5/, then 3.4/, 3.3/, ...
for v in ['2and3', str(pyversion[0])] + versions:
for lib_type in ['stdlib', 'builtins', 'third_party']:
stubdir = os.path.join(data_dir, 'typeshed', lib_type, v)
if os.path.isdir(stubdir):
path.append(stubdir)

third_party_stubdir = os.path.join(data_dir, 'stubs', 'third-party-' + v)
if os.path.isdir(third_party_stubdir):
path.append(third_party_stubdir)

# Add fallback path that can be used if we have a broken installation.
if sys.platform != 'win32':
path.append('/usr/local/lib/mypy')

# Contents of Python's sys.path go last, to prefer the stubs
# TODO: To more closely model what Python actually does, builtins should
# go first, then sys.path, then anything in stdlib and third_party.
if python_path:
path.extend(sys.path)

Expand Down
9 changes: 5 additions & 4 deletions runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,16 @@ def add_stubs(driver: Driver) -> None:
# Only test each module once, for the latest Python version supported.
# The third-party stub modules will only be used if it is not in the version.
seen = set() # type: Set[str]
for version in driver.versions:
for pfx in ['', 'third-party-']:
stubdir = join('stubs', pfx + version)
# TODO: This should also test Python 2, and pass pyversion accordingly.
for version in ["2and3", "3", "3.3", "3.4", "3.5"]:
for stub_type in ['builtins', 'stdlib', 'third_party']:
stubdir = join('typeshed', stub_type, version)
for f in find_files(stubdir, suffix='.pyi'):
module = file_to_module(f[len(stubdir) + 1:])
if module not in seen:
seen.add(module)
driver.add_mypy_string(
'stub (%s) module %s' % (pfx + version, module),
'stub (%s) module %s' % (stubdir, module),
'import typing, %s' % module)


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def find_data_files(base, globs):

data_files = []

data_files += find_data_files('stubs', ['*.py', '*.pyi'])
data_files += find_data_files('typeshed', ['*.py', '*.pyi'])

data_files += find_data_files('xml', ['*.xsd', '*.xslt', '*.css'])

Expand Down
29 changes: 0 additions & 29 deletions stubs/2.7/Queue.pyi

This file was deleted.

28 changes: 0 additions & 28 deletions stubs/2.7/StringIO.pyi

This file was deleted.

11 changes: 0 additions & 11 deletions stubs/2.7/UserDict.pyi

This file was deleted.

9 changes: 0 additions & 9 deletions stubs/2.7/__future__.pyi

This file was deleted.

12 changes: 0 additions & 12 deletions stubs/2.7/_random.pyi

This file was deleted.

5 changes: 0 additions & 5 deletions stubs/2.7/abc.pyi

This file was deleted.

Loading