8000 [stubgenc] fixes and typos by sizmailov · Pull Request #9877 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

[stubgenc] fixes and typos #9877

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

Merged
merged 6 commits into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/test_stubgenc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Test stubgenc on pybind11-mypy-demo

on:
push:
branches: [master]
tags: ['*']
pull_request:
paths:
- 'misc/test-stubgenc.sh'
- 'mypy/stubgenc.py'
- 'mypy/stubdoc.py'
- 'test-data/stubgen/**'

jobs:
stubgenc:
# Check stub file generation for a small pybind11 project
# (full text match is required to pass)
runs-on: ubuntu-latest
steps:

- uses: actions/checkout@v2

- name: initialize submodules
run: git submodule update --init

- name: Setup 🐍 3.8
uses: actions/setup-python@v2
with:
python-version: 3.8

- name: Test stubgenc
run: misc/test-stubgenc.sh
14 changes: 14 additions & 0 deletions misc/test-stubgenc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
# This script is expected to be run from root of the mypy repo

# Install dependencies, demo project and mypy
python -m pip install -r test-requirements.txt
python -m pip install pybind11-mypy-demo==0.0.1
python -m pip install .

# Remove expected stubs and generate new inplace
rm -rf test-data/stubgen/pybind11_mypy_demo
stubgen -p pybind11_mypy_demo -o test-data/stubgen/

# Compare generated stubs to expected ones
git diff --exit-code test-data/stubgen/pybind11_mypy_demo
5 changes: 3 additions & 2 deletions mypy/stubdoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ def add_token(self, token: tokenize.TokenInfo) -> None:
self.state.pop()
elif self.state[-1] == STATE_ARGUMENT_LIST:
self.arg_name = self.accumulator
if not _ARG_NAME_RE.match(self.arg_name):
if not (token.string == ')' and self.accumulator.strip() == '') \
and not _ARG_NAME_RE.match(self.arg_name):
# Invalid argument name.
self.reset()
return
Expand Down Expand Up @@ -235,7 +236,7 @@ def is_unique_args(sig: FunctionSig) -> bool:
"""return true if function argument names are unique"""
return len(sig.args) == len(set((arg.name for arg in sig.args)))

# Return only signatures that have unique argument names. Mypy fails on non-uniqnue arg names.
# Return only signatures that have unique argument names. Mypy fails on non-unique arg names.
return [sig for sig in sigs if is_unique_args(sig)]


Expand Down
24 changes: 16 additions & 8 deletions mypy/stubgenc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
infer_arg_sig_from_anon_docstring, infer_ret_type_sig_from_anon_docstring, FunctionSig
)


# Members of the typing module to consider for importing by default.
_DEFAULT_TYPING_IMPORTS = (
'Any',
'Callable',
'Dict',
'Iterable',
'Iterator',
Expand Down Expand Up @@ -231,6 +231,8 @@ def strip_or_import(typ: str, module: ModuleType, imports: List[str]) -> str:
stripped_type = typ[len('builtins') + 1:]
else:
imports.append('import %s' % (arg_module,))
if stripped_type == 'NoneType':
stripped_type = 'None'
return stripped_type


Expand Down Expand Up @@ -365,14 +367,20 @@ def method_name_sort_key(name: str) -> Tuple[int, str]:
return 1, name


def is_pybind_skipped_attribute(attr: str) -> bool:
return attr.startswith("__pybind11_module_local_")


def is_skipped_attribute(attr: str) -> bool:
return attr in ('__getattribute__',
'__str__',
'__repr__',
'__doc__',
'__dict__',
'__module__',
'__weakref__') # For pickling
return (attr in ('__getattribute__',
'__str__',
'__repr__',
'__doc__',
'__dict__',
'__module__',
'__weakref__') # For pickling
or is_pybind_skipped_attribute(attr)
)


def infer_method_sig(name: str) -> List[ArgSig]:
Expand Down
Empty file.
48 changes: 48 additions & 0 deletions test-data/stubgen/pybind11_mypy_demo/basics.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from typing import Any

from typing import overload
PI: float

def answer() -> int: ...
def midpoint(left: float, right: float) -> float: ...
def sum(arg0: int, arg1: int) -> int: ...
def weighted_midpoint(left: float, right: float, alpha: float = ...) -> float: ...

class Point:
AngleUnit: Any = ...
LengthUnit: Any = ...
origin: Any = ...
@overload
def __init__(self) -> None: ...
@overload
def __init__(self, x: float, y: float) -> None: ...
@overload
def __init__(*args, **kwargs) -> Any: ...
@overload
def distance_to(self, x: float, y: float) -> float: ...
@overload
def distance_to(self, other: Point) -> float: ...
@overload
def distance_to(*args, **kwargs) -> Any: ...
@property
def angle_unit(self) -> pybind11_mypy_demo.basics.Point.AngleUnit: ...
@angle_unit.setter
def angle_unit(self, val: pybind11_mypy_demo.basics.Point.AngleUnit) -> None: ...
@property
def length(self) -> float: ...
@property
def length_unit(self) -> pybind11_mypy_demo.basics.Point.LengthUnit: ...
@length_unit.setter
def length_unit(self, val: pybind11_mypy_demo.basics.Point.LengthUnit) -> None: ...
@property
def x(self) -> float: ...
@x.setter
def x(self, val: float) -> None: ...
@property
def x_axis(self) -> pybind11_mypy_demo.basics.Point: ...
@property
def y(self) -> float: ...
@y.setter
def y(self, val: float) -> None: ...
@property
def y_axis(self) -> pybind11_mypy_demo.basics.Point: ...
3972
0