Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First pass mypy typing #387

Merged
merged 18 commits into from
Jun 14, 2022
1 change: 1 addition & 0 deletions cunumeric/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

:meta private:
"""
from __future__ import annotations

import numpy as _np

Expand Down
1 change: 1 addition & 0 deletions cunumeric/_sphinxext/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from __future__ import annotations

from typing import TypedDict

Expand Down
4 changes: 2 additions & 2 deletions cunumeric/_sphinxext/_comparison_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@
class SectionConfig:
title: str
attr: str | None
types: tuple(type) | None = None
names: tuple(str) | None = None
types: tuple[type, ...] | None = None
names: tuple[str, ...] | None = None


FUNCTIONS = (FunctionType, BuiltinFunctionType)
Expand Down
22 changes: 16 additions & 6 deletions cunumeric/_sphinxext/_comparison_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@

from dataclasses import dataclass
from types import ModuleType
from typing import TYPE_CHECKING, Any, Iterable, Iterator, Type, Union

from ..coverage import is_implemented, is_multi, is_single
from ._comparison_config import MISSING_NP_REFS, SKIP

if TYPE_CHECKING:
from ._comparison_config import SectionConfig

YES = "\u2713"
NO = "\u274C"

Expand All @@ -42,7 +46,7 @@ class SectionDetail:
items: list[ItemDetail]


def _npref(name, obj):
def _npref(name: str, obj: Any) -> str:
if isinstance(obj, ModuleType):
full_name = f"{obj.__name__}.{name}"
else:
Expand All @@ -55,7 +59,7 @@ def _npref(name, obj):
return f":{role}:`{full_name}`"


def _lgref(name, obj, implemented):
def _lgref(name: str, obj: Any, implemented: bool) -> str:
if not implemented:
return "-"

Expand All @@ -69,7 +73,11 @@ def _lgref(name, obj, implemented):
return f":{role}:`{full_name}`"


def filter_names(obj, types=None, skip=()):
def filter_names(
obj: Any,
types: Union[tuple[Type[Any], ...], None] = None,
skip: Iterable[str] = (),
) -> Iterator[str]:
names = (n for n in dir(obj)) # every name in the module or class
names = (n for n in names if n not in skip) # except the ones we skip
names = (n for n in names if not n.startswith("_")) # or any private names
Expand All @@ -79,7 +87,7 @@ def filter_names(obj, types=None, skip=()):
return names


def get_item(name, np_obj, lg_obj):
def get_item(name: str, np_obj: Any, lg_obj: Any) -> ItemDetail:
lg_attr = getattr(lg_obj, name)

if implemented := is_implemented(lg_attr):
Expand All @@ -98,7 +106,7 @@ def get_item(name, np_obj, lg_obj):
)


def get_namespaces(attr):
def get_namespaces(attr: Union[str, None]) -> tuple[Any, Any]:
import numpy

import cunumeric
Expand All @@ -109,9 +117,11 @@ def get_namespaces(attr):
return getattr(numpy, attr), getattr(cunumeric, attr)


def generate_section(config):
def generate_section(config: SectionConfig) -> SectionDetail:
np_obj, lg_obj = get_namespaces(config.attr)

names: Iterable[str]

if config.names:
names = config.names
else:
Expand Down
2 changes: 1 addition & 1 deletion cunumeric/_sphinxext/_cunumeric_directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@


class CunumericDirective(SphinxDirective):
def parse(self, rst_text, annotation):
def parse(self, rst_text: str, annotation: str) -> nodes.Node:
result = ViewList()
for line in rst_text.split("\n"):
result.append(line, annotation)
Expand Down
8 changes: 5 additions & 3 deletions cunumeric/_sphinxext/comparison_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
#
from __future__ import annotations

from docutils import nodes
from docutils.parsers.rst.directives import choice
from sphinx.application import Sphinx
from sphinx.util.logging import getLogger

from . import PARALLEL_SAFE
from . import PARALLEL_SAFE, SphinxParallelSpec
from ._comparison_config import GROUPED_CONFIGS, NUMPY_CONFIGS
from ._comparison_util import generate_section
from ._cunumeric_directive import CunumericDirective
Expand All @@ -36,7 +38,7 @@ class ComparisonTable(CunumericDirective):
"sections": lambda x: choice(x, ("numpy", "grouped")),
}

def run(self):
def run(self) -> nodes.Node:
if self.options.get("sections", "numpy") == "numpy":
section_configs = NUMPY_CONFIGS
else:
Expand All @@ -50,6 +52,6 @@ def run(self):
return self.parse(rst_text, "<comparison-table>")


def setup(app):
def setup(app: Sphinx) -> SphinxParallelSpec:
app.add_directive_to_domain("py", "comparison-table", ComparisonTable)
return PARALLEL_SAFE
12 changes: 9 additions & 3 deletions cunumeric/_sphinxext/ufunc_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from __future__ import annotations

from typing import Any

from sphinx.application import Sphinx
from sphinx.ext.autodoc import FunctionDocumenter

from cunumeric import ufunc

from . import PARALLEL_SAFE
from . import PARALLEL_SAFE, SphinxParallelSpec


class UfuncDocumenter(FunctionDocumenter):
Expand All @@ -26,10 +30,12 @@ class UfuncDocumenter(FunctionDocumenter):
priority = 20

@classmethod
def can_document_member(cls, member, membername, isattr, parent):
def can_document_member(
cls, member: Any, membername: str, isattr: bool, parent: Any
) -> bool:
return isinstance(getattr(member, "__wrapped__", None), ufunc)


def setup(app):
def setup(app: Sphinx) -> SphinxParallelSpec:
app.add_autodocumenter(UfuncDocumenter)
return PARALLEL_SAFE
1 change: 1 addition & 0 deletions cunumeric/_ufunc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from __future__ import annotations
bryevdv marked this conversation as resolved.
Show resolved Hide resolved

from .bit_twiddling import *
from .comparison import *
Expand Down
1 change: 1 addition & 0 deletions cunumeric/_ufunc/bit_twiddling.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from __future__ import annotations

from cunumeric.config import BinaryOpCode, UnaryOpCode

Expand Down
1 change: 1 addition & 0 deletions cunumeric/_ufunc/comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from __future__ import annotations

from cunumeric.config import BinaryOpCode, UnaryOpCode, UnaryRedCode

Expand Down
1 change: 1 addition & 0 deletions cunumeric/_ufunc/floating.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from __future__ import annotations

from cunumeric.config import BinaryOpCode, UnaryOpCode

Expand Down
1 change: 1 addition & 0 deletions cunumeric/_ufunc/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from __future__ import annotations

from cunumeric.config import BinaryOpCode, UnaryOpCode, UnaryRedCode

Expand Down
1 change: 1 addition & 0 deletions cunumeric/_ufunc/trigonometric.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from __future__ import annotations

from cunumeric.config import BinaryOpCode, UnaryOpCode

Expand Down
Loading