Skip to content

Commit ba322c1

Browse files
authored
Move to .pyi and fix accessing attributes through class (#3)
1 parent 9bba9da commit ba322c1

12 files changed

+193
-21
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repos:
44
hooks:
55
- id: reorder-python-imports
66
- repo: https://github.com/pre-commit/pre-commit-hooks
7-
rev: v2.0.0
7+
rev: v2.1.0
88
hooks:
99
- id: check-ast
1010
- id: check-docstring-first

pynamodb_attributes/_typing.pyi

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from typing import overload, Any, TypeVar
2+
3+
import pynamodb.attributes
4+
5+
_A = TypeVar('_A', bound=pynamodb.attributes.Attribute)
6+
_T = TypeVar('_T')
7+
8+
9+
class Attribute(pynamodb.attributes.Attribute[_T]):
10+
@overload
11+
def __get__(self: _A, instance: None, owner: Any) -> _A: ...
12+
@overload
13+
def __get__(self, instance: Any, owner: Any) -> _T: ...

pynamodb_attributes/integer.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
from typing import Any
2-
from typing import TYPE_CHECKING
3-
41
from pynamodb.attributes import Attribute
52
from pynamodb.attributes import NumberAttribute
63

@@ -12,7 +9,3 @@ class IntegerAttribute(Attribute):
129
attr_type = NumberAttribute.attr_type
1310
serialize = NumberAttribute.serialize
1411
deserialize = NumberAttribute.deserialize
15-
16-
if TYPE_CHECKING:
17-
def __get__(self, instance: Any, owner: Any) -> int:
18-
...

pynamodb_attributes/integer.pyi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from ._typing import Attribute
2+
3+
4+
class IntegerAttribute(Attribute[int]):
5+
...

pynamodb_attributes/integer_date.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import json
22
from datetime import date
3-
from typing import Any
4-
from typing import TYPE_CHECKING
53

64
from pynamodb.attributes import Attribute
75

@@ -18,7 +16,3 @@ def serialize(self, value: date) -> str:
1816
def deserialize(self, value: str) -> date:
1917
n = json.loads(value)
2018
return date(n // 1_00_00, n // 1_00 % 1_00, n % 1_00)
21-
22-
if TYPE_CHECKING:
23-
def __get__(self, instance: Any, owner: Any) -> date:
24-
...

pynamodb_attributes/integer_date.pyi

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from datetime import date
2+
3+
from ._typing import Attribute
4+
5+
6+
class IntegerDateAttribute(Attribute[date]):
7+
...
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from typing import Any
2+
from typing import Type
3+
from typing import TypeVar
4+
5+
from ._typing import Attribute
6+
7+
_T = TypeVar('_T', bound=tuple)
8+
9+
10+
class UnicodeDelimitedTupleAttribute(Attribute[_T]):
11+
def __init__(self, tuple_type: Type[_T], delimiter: str = ..., **kwargs: Any) -> None:
12+
...

pynamodb_attributes/unicode_enum.pyi

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from enum import Enum
2+
from typing import Any
3+
from typing import Optional
4+
from typing import Type
5+
from typing import TypeVar
6+
7+
from ._typing import Attribute
8+
9+
_T = TypeVar('_T', bound=Enum)
10+
11+
12+
class UnicodeEnumAttribute(Attribute[_T]):
13+
def __init__(self, enum_type: Type[_T], unknown_value: Optional[_T] = ..., **kwargs: Any) -> None:
14+
...

requirements.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
pre-commit
2-
pytest
3-
pytest-cov
4-
pytest-mock
1+
mypy>=0.650
2+
pre-commit>=1.13.0
3+
pytest>=4.1.1
4+
pytest-cov>=2.6.1
5+
pytest-mock>=1.10.0

setup.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
1+
import os
2+
13
from setuptools import find_packages
24
from setuptools import setup
35

6+
7+
def find_stubs(package): # type: ignore
8+
stubs = []
9+
for root, _, files in os.walk(package):
10+
for f in files:
11+
path = os.path.join(root, f).replace(package + os.sep, '', 1)
12+
if path.endswith('.pyi') or path.endswith('py.typed'):
13+
stubs.append(path)
14+
return {package: stubs}
15+
16+
417
setup(
518
name='pynamodb-attributes',
6-
version='0.1.2',
19+
version='0.2.0',
720
description='Common attributes for PynamoDB',
821
url='https://www.github.com/lyft/pynamodb-attributes',
922
maintainer='Lyft',
1023
maintainer_email='[email protected]',
1124
packages=find_packages(exclude=['tests*']),
1225
dependency_links=[],
1326
install_requires=[
14-
"pynamodb",
27+
"pynamodb>=3.3.3",
1528
],
1629
python_requires='>=3',
17-
package_data={'pynamodb_attributes': ['py.typed']},
30+
package_data=find_stubs('pynamodb_attributes'),
1831
)

0 commit comments

Comments
 (0)