mirror of
https://github.com/davidhalter/typeshed.git
synced 2026-01-09 21:12:25 +08:00
76 lines
2.6 KiB
Markdown
76 lines
2.6 KiB
Markdown
# typeshed
|
|
|
|
## About
|
|
|
|
Typeshed models function types for the Python standard library
|
|
and Python builtins, as well as third party packages.
|
|
|
|
This data can e.g. be used for static analysis, type checking or type inference.
|
|
|
|
## Format
|
|
|
|
Each Python module is represented by a `.pyi` "stub". This is a normal Python
|
|
file (i.e., it can be interpreted by Python 3), except all the methods are empty.
|
|
Python function annotations ([PEP 3107](https://www.python.org/dev/peps/pep-3107/))
|
|
are used to describe the types the function has.
|
|
See [PEP 484](http://www.python.org/dev/peps/pep-0484/) for the exact syntax
|
|
of the stub files.
|
|
|
|
## Directory structure
|
|
|
|
We store stubs for both Python 2 as well as Python 3. We also distinguish
|
|
between minor versions (E.g. 3.2 <-> 3.3). To accomplish not having to duplicate
|
|
modules that are the same between all minor versions, we have e.g. a top-level
|
|
directory 3/ that contains all the stubs for Python 3. More specialized stubs
|
|
go into e.g. 3.3/ and supersede the more generic stubs in 3/. (And, if needed,
|
|
a directory 3.3.1/ would be able to supersede stubs in 3.3/):
|
|
|
|
Directory | Contents
|
|
------------- | -------------
|
|
2.7/ | Stubs for Python 2.7
|
|
3/ | Stubs for Python 3
|
|
3.3/ | Some specialized stubs for Python 3.3 (replacing generic stubs in 3/)
|
|
|
|
## Example
|
|
|
|
The below is an excerpt from the types for the `datetime` module.
|
|
|
|
```
|
|
MAXYEAR = ... # type: int
|
|
MINYEAR = ... # type: int
|
|
__doc__ = ... # type: str
|
|
__file__ = ... # type: str
|
|
__name__ = ... # type: str
|
|
__package__ = ... # type: None
|
|
|
|
class date(object):
|
|
def __init__(self, year: int, month: int, day: int): ...
|
|
@classmethod
|
|
def fromtimestamp(cls, timestamp: int or float) -> date: ...
|
|
@classmethod
|
|
def fromordinal(cls, ordinal: int) -> date: ...
|
|
@classmethod
|
|
def today(self) -> date: ...
|
|
def ctime(self) -> str: ...
|
|
def weekday(self) -> int: ...
|
|
```
|
|
|
|
## About builtins vs stdlib
|
|
|
|
C extensions that are built into Python (E.g. sys, array, math, signal, ...) have
|
|
higher precedence in the import path than modules that ship with Python but
|
|
are not built into the python binary (like os.py, glob.py, zipfile.py etc.;
|
|
but also some C extensions like datetime). The
|
|
former are implicitly prepended to the start of your PYTHONPATH, whereas the
|
|
latter are implicitly appended to it.
|
|
|
|
Typeshed doesn't explicitly distinguish between the two module types. For a specific
|
|
Python build, you can use `sys.builtin_module_names` to query which modules
|
|
are built in and set import priority accordingly.
|
|
|
|
## Contributions
|
|
|
|
We're welcoming contributions (pull requests) for types of third party
|
|
packages. They'll go under {2.7,3}/dist-packages/.
|
|
|