Merge style guide from README.md into CONTRIBUTING.md (#1696)

This commit is contained in:
Sebastian Rittau
2017-11-08 18:02:59 +01:00
committed by Jelle Zijlstra
parent 0a9a2b6470
commit 9656febaee
2 changed files with 41 additions and 45 deletions

View File

@@ -49,51 +49,9 @@ 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.
## Syntax example
The below is an excerpt from the types for the `datetime` module.
```python
from typing import Union
MAXYEAR = ... # type: int
MINYEAR = ... # type: int
class date(object):
def __init__(self, year: int, month: int, day: int) -> None: ...
@classmethod
def fromtimestamp(cls, timestamp: float) -> date: ...
@classmethod
def today(cls) -> date: ...
@classmethod
def fromordinal(cls, ordinal: int) -> date: ...
def replace(self, year: int = ..., month: int = ..., day: int = ...) -> date: ...
def ctime(self) -> str: ...
def weekday(self) -> int: ...
```
## Conventions
* At the time of this writing, `unicode` arguments, in Python 2 stubs, are
interpreted by type-checkers as `Union[bytes, unicode]` (so it means the same
as `Text`).
Even so, in Python 2, whenever possible, use `unicode` if that's the only
possible type, and `Text` if it can be either `unicode` or `bytes`.
* For arguments with default values, use `...` instead of the actual
default value.
* Most type-checkers interpret optional parameters of the form `x : Foo = None`
as `x : Optional[Foo] = ...`. (So the former is a shortcut for the latter)
In typeshed, however, we prefer the explicit latter form.
* When something is declared as taking only float, it also takes `int`. See
https://www.python.org/dev/peps/pep-0484/#the-numeric-tower. So write `float`
instead of `Union[int, float]`.
* Avoid Union return types: https://github.com/python/mypy/issues/1693
* Avoid invariant collection types (List, Dict) in argument positions, in favor
of covariant types like Mapping or Sequence.
See [PEP 484](http://www.python.org/dev/peps/pep-0484/) for the exact
syntax of the stub files and [CONTRIBUTING.md](CONTRIBUTING.md) for the
coding style used in typeshed.
## Directory structure