Skip to content

Commit aa33bab

Browse files
Enable MyPy (pantsbuild#47)
This shows off the MyPy Protobuf plugin and partitioning based on interpreter constraints.
1 parent 87a6541 commit aa33bab

11 files changed

Lines changed: 65 additions & 8 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ install:
2222

2323
script:
2424
# We also smoke test that our release process will work by running `package`.
25-
- ./pants lint test package '::'
25+
- ./pants lint typecheck test package '::'
2626
- ./pants run helloworld/main.py
2727
- ./pants run helloworld/main_py2.py

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,23 @@ Try these out in this repo!
111111
## List targets
112112

113113
```
114-
./pants list helloworld:: # All targets.
114+
./pants list :: # All targets.
115115
./pants list 'helloworld/**/*.py' # Just targets containing Python code.
116116
```
117117

118118
## Run linters and formatters
119119

120120
```
121-
./pants lint helloworld::
121+
./pants lint ::
122122
./pants fmt 'helloworld/**/*.py'
123123
```
124124

125+
## Run MyPy
126+
127+
```
128+
./pants typecheck ::
129+
```
130+
125131
## Run tests
126132

127133
```
File renamed without changes.

build-support/mypy.ini

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[mypy]
2+
# Optionals
3+
no_implicit_optional = True
4+
5+
# Strictness
6+
allow_untyped_globals = False
7+
allow_redefinition = False
8+
implicit_reexport = False
9+
strict_equality = True
10+
11+
# Warnings
12+
warn_unused_ignores = True
13+
warn_no_return = True
14+
warn_return_any = True
15+
warn_redundant_casts = True
16+
warn_unreachable = True
17+
18+
# Error output
19+
show_column_numbers = True
20+
show_error_context = True
21+
show_error_codes = True
22+
show_traceback = True
23+
pretty = True
24+
color_output = True
25+
error_summary = True
26+
27+
[mypy-colors]
28+
ignore_missing_imports = True
29+
30+
[mypy-translate]
31+
ignore_missing_imports = True
32+
33+
[mypy-pytest]
34+
ignore_missing_imports = True

helloworld/greet/__init__.py

Whitespace-only changes.

helloworld/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
def say_hello() -> None:
1111
config = load_config()
12-
greeter = Greeter(languages=config.languages, greetings=config.greetings)
12+
greeter = Greeter(
13+
languages=list(config.languages), greetings=list(config.greetings)
14+
)
1315
sentence = greeter.greet("world")
1416
print(green(sentence))
1517

helloworld/util/__init__.py

Whitespace-only changes.

helloworld/util/lang.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Licensed under the Apache License, Version 2.0 (see LICENSE).
33

44
import random
5-
from typing import List
5+
from typing import List, cast
66

77
from translate import Translator
88

@@ -18,7 +18,7 @@ def translate(self, lang: str, phrase: str) -> str:
1818
if lang not in self._langs:
1919
raise self.UnknownLanguage(lang)
2020
translator = Translator(lang)
21-
return translator.translate(phrase)
21+
return cast(str, translator.translate(phrase))
2222

2323
def translate_to_random_language(self, phrase: str) -> str:
2424
return self.translate(self._pick_random_language(), phrase)

helloworld/util/proto/BUILD

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,14 @@
33

44
# `name` defaults to the name of this directory, i.e., `proto`.
55
# `sources` defaults to ["*.proto"].
6-
protobuf_library()
6+
protobuf_library(dependencies=[":init"])
7+
8+
9+
# Note that we need an `__init__.py` file for MyPy to recognize our generated code as a valid
10+
# module. We include this in the dependencies for the `protobuf_library` to ensure the
11+
# `__init__.py` is always used.
12+
#
13+
# Often, you won't want to put `__init__.py` files in your Protobuf directories; see
14+
# https://www.pantsbuild.org/docs/protobuf#protobuf-and-source-roots for how to change where the
15+
# protobuf code is generated.
16+
python_library(name="init")

helloworld/util/proto/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)