Skip to content

Commit f25851a

Browse files
authored
Clean up some mypy errors (#123)
* Use ellipsis instead of pass when dealing with type stubs Gets rid of some IDE warnings due to functions not returning what they annotate as their return values * Clean up some random type warnings * Clean up some mypy errors by adding real return statements * black formatting
1 parent a8adf32 commit f25851a

File tree

4 files changed

+33
-43
lines changed

4 files changed

+33
-43
lines changed

.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
__pycache__
2-
venv
2+
venv*
3+
.venv
34
*.egg-info
5+
build
6+
dist
7+
*.iml

test/async_wrap_test.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ def bar():
3131

3232
def test_wrap_asynccontextmanager_annotations():
3333
@async_wrap.asynccontextmanager # this would not work with contextlib.asynccontextmanager
34-
async def foo() -> typing.AsyncGenerator[int, None]:
35-
pass
34+
async def foo() -> typing.AsyncGenerator[int, None]: ...
3635

3736
assert foo.__annotations__["return"] == typing.AsyncContextManager[int]
3837

test/type_stub_helpers/e2e_example_impl.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,18 @@ async def gen(self) -> AsyncGenerator[int, None]:
1717
yield 1
1818

1919
@staticmethod
20-
def some_static(arg: str) -> float:
21-
pass
20+
def some_static(arg: str) -> float: ...
2221

2322
@classmethod
2423
def clone(cls, foo: "_Foo") -> "_Foo": # self ref
25-
pass
24+
...
2625

2726

2827
_T = TypeVar("_T", bound=_Foo)
2928

3029

3130
async def _listify(t: _T) -> List[_T]:
32-
return t
31+
return [t]
3332

3433

3534
@overload
@@ -53,5 +52,5 @@ async def _returns_foo() -> _Foo:
5352

5453

5554
@asynccontextmanager
56-
def make_context(a: float) -> typing.AsyncGenerator[str, None]:
55+
async def make_context(a: float) -> typing.AsyncGenerator[str, None]:
5756
yield "hello"

test/type_stub_test.py

+23-35
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,17 @@
99
from .type_stub_helpers import some_mod
1010

1111

12-
def noop():
13-
pass
12+
def noop(): ...
1413

1514

16-
def arg_no_anno(arg1):
17-
pass
15+
def arg_no_anno(arg1): ...
1816

1917

2018
def scalar_args(arg1: str, arg2: int) -> float:
21-
pass
19+
return 0
2220

2321

24-
def generic_other_module_arg(arg: typing.List[some_mod.Foo]):
25-
pass
22+
def generic_other_module_arg(arg: typing.List[some_mod.Foo]): ...
2623

2724

2825
async def async_func() -> str:
@@ -154,8 +151,7 @@ def test_wrapped_function_with_new_annotations():
154151
This test makes sure we do just that.
155152
"""
156153

157-
def orig(arg: str):
158-
pass
154+
def orig(arg: str): ...
159155

160156
@functools.wraps(orig)
161157
def wrapper(extra_arg: int, *args, **kwargs):
@@ -167,7 +163,7 @@ def wrapper(extra_arg: int, *args, **kwargs):
167163

168164
class Base:
169165
def base_method(self) -> str:
170-
pass
166+
return ""
171167

172168

173169
Base.__module__ = "basemod"
@@ -176,7 +172,7 @@ def base_method(self) -> str:
176172

177173
class Based(Base):
178174
def sub(self) -> float:
179-
pass
175+
return 0
180176

181177

182178
def test_base_class_included_and_imported():
@@ -191,7 +187,7 @@ def test_typevar():
191187
T.__module__ = "source_mod"
192188

193189
def foo(arg: T) -> T:
194-
pass
190+
return arg
195191

196192
src = _function_source(foo)
197193
assert "import source_mod" in src
@@ -206,12 +202,10 @@ def test_string_annotation():
206202

207203

208204
class Forwarder:
209-
def foo(self) -> typing.Optional["Forwardee"]:
210-
pass
205+
def foo(self) -> typing.Optional["Forwardee"]: ...
211206

212207

213-
class Forwardee:
214-
pass
208+
class Forwardee: ...
215209

216210

217211
def test_forward_ref():
@@ -228,7 +222,7 @@ def test_forward_ref():
228222

229223
class SelfRefFoo:
230224
def foo(self) -> "SelfRefFoo":
231-
pass
225+
return self
232226

233227

234228
def test_self_ref():
@@ -241,7 +235,7 @@ def test_self_ref():
241235
class _Foo:
242236
@staticmethod
243237
async def clone(foo: "_Foo") -> "_Foo":
244-
pass
238+
return foo
245239

246240

247241
synchronizer = synchronicity.Synchronizer()
@@ -250,7 +244,7 @@ async def clone(foo: "_Foo") -> "_Foo":
250244

251245
def test_synchronicity_type_translation():
252246
async def _get_foo(foo: _Foo) -> _Foo:
253-
pass
247+
return foo
254248

255249
get_foo = synchronizer.create_blocking(_get_foo, "get_foo", __name__)
256250
src = _function_source(get_foo)
@@ -271,8 +265,7 @@ def test_synchronicity_self_ref():
271265

272266
class _WithClassMethod:
273267
@classmethod
274-
def classy(cls):
275-
pass
268+
def classy(cls): ...
276269

277270
async def meth(self, arg: bool) -> int:
278271
return 0
@@ -306,8 +299,7 @@ async def aio(self, *args, **kwargs) -> int:
306299
T = typing.TypeVar("T")
307300

308301

309-
class MyGeneric(typing.Generic[T]):
310-
pass
302+
class MyGeneric(typing.Generic[T]): ...
311303

312304

313305
BlockingGeneric = synchronizer.create_blocking(typing.Generic, "BlockingGeneric", __name__)
@@ -320,16 +312,14 @@ class MyGeneric(typing.Generic[T]):
320312

321313
def test_custom_generic():
322314
# TODO: build out this test a bit, as it currently creates an invalid stub (missing base types)
323-
class Specific(MyGeneric[str]):
324-
pass
315+
class Specific(MyGeneric[str]): ...
325316

326317
src = _class_source(Specific)
327318
assert "class Specific(MyGeneric[str]):" in src
328319

329320

330321
def test_synchronicity_generic_subclass():
331-
class Specific(MyGeneric[str]):
332-
pass
322+
class Specific(MyGeneric[str]): ...
333323

334324
assert Specific.__bases__ == (MyGeneric,)
335325
assert Specific.__orig_bases__ == (MyGeneric[str],)
@@ -338,8 +328,7 @@ class Specific(MyGeneric[str]):
338328
src = _class_source(BlockingSpecific)
339329
assert "class BlockingSpecific(BlockingMyGeneric[str]):" in src
340330

341-
async def foo_impl(bar: MyGeneric[str]):
342-
pass
331+
async def foo_impl(bar: MyGeneric[str]): ...
343332

344333
foo = synchronizer.create_blocking(foo_impl, "foo")
345334
src = _function_source(foo)
@@ -372,14 +361,15 @@ def test_translated_bound_type_vars():
372361

373362
def test_ellipsis():
374363
def foo() -> typing.Callable[..., typing.Any]:
375-
pass
364+
return lambda x: 0
376365

377366
src = _function_source(foo)
378367
assert "-> typing.Callable[..., typing.Any]" in src
379368

380369

381370
def test_typing_literal():
382-
def foo() -> typing.Literal["three", "str"]: ...
371+
def foo() -> typing.Literal["three", "str"]:
372+
return "str"
383373

384374
src = _function_source(foo)
385375
assert "-> typing.Literal['three', 'str']" in src # "str" should not be eval:ed in a Literal!
@@ -389,12 +379,10 @@ def test_overloads_unwrapped_functions():
389379
with overload_tracking.patched_overload():
390380

391381
@typing.overload
392-
def _overloaded(arg: str) -> float:
393-
pass
382+
def _overloaded(arg: str) -> float: ...
394383

395384
@typing.overload
396-
def _overloaded(arg: int) -> int:
397-
pass
385+
def _overloaded(arg: int) -> int: ...
398386

399387
def _overloaded(arg: typing.Union[str, int]):
400388
if isinstance(arg, str):

0 commit comments

Comments
 (0)