Skip to content

Commit

Permalink
Build stubs with ast (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
unmade committed Aug 29, 2022
1 parent 2b4dd6f commit 705c3e0
Show file tree
Hide file tree
Showing 44 changed files with 1,291 additions and 541 deletions.
3 changes: 1 addition & 2 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ source =
parallel = true

[report]
fail_under = 100
show_missing = true
precision = 2
omit = *__main__*
omit = *__main__*
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PYTHONPATH="src:${PYTHONPATH}"
45 changes: 45 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: lint

on:
push:
branches:
- master
pull_request:
branches:
- '**'


jobs:

lint:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up Python
id: set-python
uses: actions/setup-python@v4
with:
python-version: "3.7" # don't forget to update cache key below

- name: Cache pip dependencies
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-3.7-pip-${{ hashFiles('**/poetry.lock') }}

- name: Cache pre-commit dependencies
uses: actions/cache@v3
with:
path: ~/.cache/pre-commit
key: ${{ runner.os }}-pre-commit-${{ hashFiles('**/.pre-commit-config.yaml') }}-${{ hashFiles('**/tox.ini') }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install tox
- name: Lint
run: |
tox -e lint
18 changes: 7 additions & 11 deletions .github/workflows/lint-and-test.yml → .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: lint and test
name: test

on:
push:
Expand All @@ -16,24 +16,24 @@ jobs:
strategy:
max-parallel: 4
matrix:
python-version: [3.7, 3.8]
python-version: ['3.7', '3.8', '3.9', '3.10']

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v3

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@master
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Cache pip dependencies
uses: actions/cache@master
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-${{ matrix.python-version }}-pip-${{ hashFiles('**/poetry.lock') }}

- name: Cache pre-commit dependencies
uses: actions/cache@master
uses: actions/cache@v3
with:
path: ~/.cache/pre-commit
key: ${{ runner.os }}-${{ matrix.python-version }}-pre-commit-${{ hashFiles('**/.pre-commit-config.yaml') }}-${{ hashFiles('**/tox.ini') }}
Expand All @@ -43,17 +43,13 @@ jobs:
python -m pip install --upgrade pip
pip install tox
- name: Lint
run: |
tox -e lint
- name: Basic Test Suit
run: |
tox -e py
- name: Generate coverage report
run: |
tox -e coverage
tox -e coverage -- --fail-under=99
- name: Upload coverage to Codecov
uses: codecov/codecov-action@master
Expand Down
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ default_language_version:
python: python3
repos:
- repo: https://github.com/ambv/black
rev: 19.3b0
rev: 22.6.0
hooks:
- id: black
- repo: https://github.com/humitos/mirrors-autoflake
rev: v1.2
rev: v1.1
hooks:
- id: autoflake
- repo: https://github.com/pre-commit/mirrors-isort
rev: v4.3.15
rev: v5.10.1
hooks:
- id: isort
additional_dependencies:
Expand Down
8 changes: 0 additions & 8 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ confidence=
# no Warning level messages displayed, use "--disable=all --enable=classes
# --disable=W".
disable=missing-docstring,
bad-continuation,
use-implicit-booleaness-not-comparison,

# Enable the message, report, category or checker with the given id(s). You can
Expand Down Expand Up @@ -246,13 +245,6 @@ max-line-length=88
# Maximum number of lines in a module.
max-module-lines=1000

# List of optional constructs for which whitespace checking is disabled. `dict-
# separator` is used to allow tabulation in dicts, etc.: {1 : 1,\n222: 2}.
# `trailing-comma` allows a space between comma and closing bracket: (a, ).
# `empty-line` allows space-only lines.
no-space-check=trailing-comma,
dict-separator

# Allow the body of a class to be on the same line as the declaration if body
# contains single statement.
single-line-class-stmt=no
Expand Down
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ For example in thrift the following will be correct declaration:
2: required string text
}
In python attributes without a default cannot follow attributes with one.
Therefore by default all fields are optional with default to None. This is compliant
to `thriftpy2 <https://github.com/Thriftpy/thriftpy2>`_.
In python, fields without default values cannot appear after fields with default
values. Therefore by default all fields are optional with default to None.
This is compliant to `thriftpy2 <https://github.com/Thriftpy/thriftpy2>`_.

However, if you want more strict behaviour you can specify `--strict-optional` option.
For the case above, the following stubs will be generated:
Expand Down
2 changes: 1 addition & 1 deletion example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ in order to have correct autocomplete and type checking for dynamically loaded t

Stubs were created with command:

thriftpyi example/interfaces --output example/app/interfaces
thriftpyi example/interfaces --output example/app/interfaces --strict-optional

Note, that [\_\_init__.py](app/interfaces/__init__.py) was created by hand and not by the script.
This file is responsible for the actual access to thrift interfaces.
Expand Down
2 changes: 1 addition & 1 deletion example/app/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

todo_id = client.create(text="item", type=interfaces.todo.TodoType.NOTE)
print(f"CREATE = {todo_id}")
print(f"GET = {client.get(id=todo_id)}")
print(f"GET = {client.get(todo_id)}")
try:
client.get(id=todo_id + 1)
except interfaces.shared.NotFound:
Expand Down
5 changes: 1 addition & 4 deletions example/app/interfaces/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
from . import dates
from . import shared
from . import todo
from . import todo_v2
from . import dates, shared, todo, todo_v2
4 changes: 2 additions & 2 deletions example/app/interfaces/dates.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from dataclasses import dataclass
from typing import *

@dataclass
class DateTime:
year: int
Expand All @@ -11,5 +12,4 @@ class DateTime:
microsecond: Optional[int] = None

@dataclass
class Date:
pass
class Date: ...
6 changes: 4 additions & 2 deletions example/app/interfaces/shared.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ from dataclasses import dataclass
from typing import *

class NotFound(Exception):
message: Optional[str] = None
def __init__(self, message: Optional[str] = "Not Found"): ...

class EmptyException(Exception): ...

@dataclass
class LimitOffset:
limit: Optional[int] = None
offset: Optional[int] = None

class Service:
def ping(self,) -> str: ...
def ping(self) -> str: ...
11 changes: 5 additions & 6 deletions example/app/interfaces/todo.pyi
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from dataclasses import dataclass
from enum import IntEnum
from typing import *

from . import shared
from . import dates
from . import shared

class TodoType(IntEnum):
PLAIN = 1
Expand All @@ -26,7 +25,7 @@ class Todo:
def get(self, id: int) -> TodoItem: ...
def all(self, pager: shared.LimitOffset) -> List[TodoItem]: ...
def filter(self, ids: List[int]) -> List[TodoItem]: ...
def stats(self,) -> Dict[int, float]: ...
def types(self,) -> Set[int]: ...
def groupby(self,) -> Dict[int, List[TodoItem]]: ...
def ping(self,) -> str: ...
def stats(self) -> Dict[int, float]: ...
def types(self) -> Set[int]: ...
def groupby(self) -> Dict[int, List[TodoItem]]: ...
def ping(self) -> str: ...
3 changes: 1 addition & 2 deletions example/app/interfaces/todo_v2.pyi
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
class Todo:
pass
class Todo: ...
Loading

0 comments on commit 705c3e0

Please sign in to comment.