From b4fa1626f79d7cbb171e2eb4701f10bdcef5e463 Mon Sep 17 00:00:00 2001 From: Jimmy Lai Date: Wed, 18 Mar 2020 21:41:48 -0700 Subject: [PATCH] [typing] add missing type annotations --- libcst/_nodes/module.py | 2 +- libcst/_nodes/statement.py | 8 ++++---- libcst/tests/test_tabs.py | 2 +- libcst/tests/test_type_enforce.py | 4 ++-- setup.py | 12 +++++++++--- stubs/hypothesmith.pyi | 1 + stubs/setuptools.pyi | 1 + 7 files changed, 19 insertions(+), 11 deletions(-) create mode 100644 stubs/hypothesmith.pyi create mode 100644 stubs/setuptools.pyi diff --git a/libcst/_nodes/module.py b/libcst/_nodes/module.py index 8df3bf9d2..400d2f6b6 100644 --- a/libcst/_nodes/module.py +++ b/libcst/_nodes/module.py @@ -159,7 +159,7 @@ def config_for_parsing(self) -> "PartialParserConfig": default_newline=self.default_newline, ) - def get_docstring(self, clean=True) -> Optional[str]: + def get_docstring(self, clean: bool = True) -> Optional[str]: """ Returns a :func:`inspect.cleandoc` cleaned docstring if the docstring is available, ``None`` otherwise. """ diff --git a/libcst/_nodes/statement.py b/libcst/_nodes/statement.py index b0ac35d3a..08db5f8f9 100644 --- a/libcst/_nodes/statement.py +++ b/libcst/_nodes/statement.py @@ -7,7 +7,7 @@ import re from abc import ABC, abstractmethod from dataclasses import dataclass -from typing import Optional, Sequence, Union +from typing import Optional, Pattern, Sequence, Union from libcst._add_slots import add_slots from libcst._maybe_sentinel import MaybeSentinel @@ -50,7 +50,7 @@ from libcst._visitors import CSTVisitorT -_INDENT_WHITESPACE_RE = re.compile(r"[ \f\t]+", re.UNICODE) +_INDENT_WHITESPACE_RE: Pattern[str] = re.compile(r"[ \f\t]+", re.UNICODE) class BaseSuite(CSTNode, ABC): @@ -1633,7 +1633,7 @@ def _codegen_impl(self, state: CodegenState) -> None: state.add_token(":") self.body._codegen(state) - def get_docstring(self, clean=True) -> Optional[str]: + def get_docstring(self, clean: bool = True) -> Optional[str]: """ When docstring is available, returns a :func:`inspect.cleandoc` cleaned docstring. Otherwise, returns ``None``. @@ -1782,7 +1782,7 @@ def _codegen_impl(self, state: CodegenState) -> None: # noqa: C901 state.add_token(":") self.body._codegen(state) - def get_docstring(self, clean=True) -> Optional[str]: + def get_docstring(self, clean: bool = True) -> Optional[str]: """ Returns a :func:`inspect.cleandoc` cleaned docstring if the docstring is available, ``None`` otherwise. """ diff --git a/libcst/tests/test_tabs.py b/libcst/tests/test_tabs.py index 1be5c421d..f72824c53 100644 --- a/libcst/tests/test_tabs.py +++ b/libcst/tests/test_tabs.py @@ -20,5 +20,5 @@ class ExpandTabsTest(UnitTest): ("\tsuffix", " suffix"), ] ) - def test_expand_tabs(self, input, output) -> None: + def test_expand_tabs(self, input: str, output: str) -> None: self.assertEqual(expand_tabs(input), output) diff --git a/libcst/tests/test_type_enforce.py b/libcst/tests/test_type_enforce.py index 5d28b69b1..b5525554f 100644 --- a/libcst/tests/test_type_enforce.py +++ b/libcst/tests/test_type_enforce.py @@ -36,7 +36,7 @@ class MyExampleChildClass(MyExampleClass): pass -class WeirdSubclassOfList(List): +class WeirdSubclassOfList(List[int]): pass @@ -130,7 +130,7 @@ class TypeEnforcementTest(UnitTest): (MyExampleClassWithMetaclass(), Any), ] ) - def test_basic_pass(self, value, expected_type) -> None: + def test_basic_pass(self, value: object, expected_type: object) -> None: self.assertTrue( is_value_of_type(value, expected_type), f"value {value!r} was supposed to be of type {expected_type!r}", diff --git a/setup.py b/setup.py index 344e3719e..8e0fa8817 100644 --- a/setup.py +++ b/setup.py @@ -3,22 +3,28 @@ # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. + import importlib.util from os import path +from typing import TYPE_CHECKING import setuptools +if TYPE_CHECKING: + from importlib.machinery import ModuleSpec + from types import ModuleType + # Grab the readme so that our package stays in sync with github. -this_directory = path.abspath(path.dirname(__file__)) +this_directory: str = path.abspath(path.dirname(__file__)) with open(path.join(this_directory, "README.rst"), encoding="utf-8") as f: long_description = f.read() # Grab the version constant so that libcst.tool stays in sync with this package. -spec = importlib.util.spec_from_file_location( +spec: "ModuleSpec" = importlib.util.spec_from_file_location( "version", path.join(this_directory, "libcst/_version.py") ) -version = importlib.util.module_from_spec(spec) +version: "ModuleType" = importlib.util.module_from_spec(spec) # pyre-ignore Pyre doesn't know about importlib entirely. spec.loader.exec_module(version) # pyre-ignore Pyre has no way of knowing that this constant exists. diff --git a/stubs/hypothesmith.pyi b/stubs/hypothesmith.pyi new file mode 100644 index 000000000..0568b4d1e --- /dev/null +++ b/stubs/hypothesmith.pyi @@ -0,0 +1 @@ +# pyre-placeholder-stub diff --git a/stubs/setuptools.pyi b/stubs/setuptools.pyi new file mode 100644 index 000000000..0568b4d1e --- /dev/null +++ b/stubs/setuptools.pyi @@ -0,0 +1 @@ +# pyre-placeholder-stub