Skip to content

Commit

Permalink
Improve Int and Float type stubs (#1656)
Browse files Browse the repository at this point in the history
This PR improves the type stubs for Int and Float, being more accurate about what's accepted for those trait types.

We need SupportsIndex, which is only available in typing from Python 3.8 onwards. So I've added typing-extensions as a dependency for traits-stubs. We can drop the dependency once we no longer support Python 3.6 and Python 3.7.
  • Loading branch information
mdickinson committed Jun 30, 2022
1 parent 80294cf commit 26e6aaa
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
7 changes: 6 additions & 1 deletion traits-stubs/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ def get_long_description():
long_description=get_long_description(),
long_description_content_type="text/x-rst",
download_url="https://pypi.python.org/pypi/traits-stubs",
install_requires=["traits"],
install_requires=[
"traits",
# We need typing-extensions for SupportsIndex; once we no longer
# support Python < 3.8, we can drop this requirement.
'typing-extensions; python_version<"3.8"',
],
extras_require={"test": ["mypy"]},
packages=[
"traits-stubs",
Expand Down
13 changes: 11 additions & 2 deletions traits-stubs/traits-stubs/trait_types.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import datetime
from pathlib import PurePath as _PurePath
import sys
from typing import (
Any as _Any,
Callable as _CallableType,
Expand All @@ -18,12 +19,20 @@ from typing import (
Optional,
Sequence as _Sequence,
Set as _SetType,
SupportsFloat,
Type as _Type,
TypeVar,
Union as _Union,
)
from uuid import UUID as _UUID

# Once we no longer support Python 3.6 or Python 3.7, we can import
# SupportsIndex from typing instead of typing_extensions.
if sys.version_info < (3, 8):
from typing_extensions import SupportsIndex
else:
from typing import SupportsIndex

from .trait_type import _TraitType

SetTypes: _Any
Expand Down Expand Up @@ -60,7 +69,7 @@ class _BaseInt(_TraitType[_T, int]):
...


class BaseInt(_BaseInt[int]):
class BaseInt(_TraitType[SupportsIndex, int]):
...


Expand All @@ -72,7 +81,7 @@ class _BaseFloat(_TraitType[_T, float]):
...


class BaseFloat(_BaseFloat[float]):
class BaseFloat(_TraitType[_Union[SupportsFloat, SupportsIndex], float]):
...


Expand Down
14 changes: 14 additions & 0 deletions traits-stubs/traits_stubs_tests/examples/Float.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
from traits.api import Float, HasTraits


class HasIndex:
"""Class with __index__ method; instances should be assignable to Float."""
def __index__(self):
return 1729


class HasFloat:
"""Class with __float__ method; instances should be assignable to Float."""
def __float__(self):
return 1729.0


class Test(HasTraits):
i = Float()

Expand All @@ -19,3 +31,5 @@ class Test(HasTraits):
o.i = "5" # E: assignment
o.i = 5
o.i = 5.5
o.i = HasIndex()
o.i = HasFloat()
7 changes: 7 additions & 0 deletions traits-stubs/traits_stubs_tests/examples/Int.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
from traits.api import HasTraits, Int


class HasIndex:
"""Class with __index__ method; instances should be assignable to Int."""
def __index__(self):
return 1729


class Test(HasTraits):
i = Int()
j = Int(default_value="234") # E: arg-type
Expand All @@ -19,6 +25,7 @@ class Test(HasTraits):

o = Test()
o.i = 5
o.i = HasIndex()

o.i = "5" # E: assignment
o.i = 5.5 # E: assignment
Expand Down

0 comments on commit 26e6aaa

Please sign in to comment.