Skip to content

Commit

Permalink
Minor fixes and updates in trait_types.pyi; new ctraits.pyi (#1661)
Browse files Browse the repository at this point in the history
This PR makes some minor type-stub related fixes and cleanups, as part of an incremental effort to get the stubs into a usable state.

* Add suitable flake8 exclusions for type stubs (it's common to put the ... on the same line as a function def, for example; black does that, but flake8 complains about it).
* Add new ctraits.pyi type stubs. It's not complete, but it's sufficient to stop complaints in some other parts of the codebase.
* Fix confusion with int being both the built-in type and an enum name in ValidateTrait
* Several fixes in trait_types.pyi. In particular, Pylance/Pyright doesn't like missing type variables, even though the typing documentation says that it should be okay to use e.g., plain Callable instead of Callable[..., Any].
* Fix some non-standard double indentation in the trait_types.pyi stubs.

Co-authored-by: Corran Webster <cwebster@enthought.com>
  • Loading branch information
mdickinson and corranwebster committed Aug 4, 2022
1 parent 7ce6475 commit 9f5fe4d
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 158 deletions.
5 changes: 4 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[flake8]
exclude = traits/observation/_generated_parser.py,build
ignore = E266,W503,E722,E731,E741
per-file-ignores = */api.py:F401
per-file-ignores =
*/api.py:F401
# Suppress flake8 complaints about black's formatting of .pyi files
*.pyi:E302,E305,E701,E704
44 changes: 25 additions & 19 deletions traits-stubs/traits-stubs/constants.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from enum import IntEnum
from typing import Any


class TraitKind(IntEnum):
trait: int = ...
python: int = ...
Expand All @@ -22,26 +23,31 @@ class TraitKind(IntEnum):
constant: int = ...
generic: int = ...


# Alias for the built-in int type, to avoid confusion with the 'int' attribute
# in the ValidateTrait namespace.
_int = int

class ValidateTrait(IntEnum):
type: int = ...
instance: int = ...
self_type: int = ...
int_range: int = ...
float_range: int = ...
enum: int = ...
map: int = ...
complex: int = ...
slow: int = ...
tuple: int = ...
prefix_map: int = ...
coerce: int = ...
cast: int = ...
function: int = ...
python: int = ...
adapt: int = ...
int: int = ...
float: int = ...
callable: int = ...
type: _int = ...
instance: _int = ...
self_type: _int = ...
int_range: _int = ...
float_range: _int = ...
enum: _int = ...
map: _int = ...
complex: _int = ...
slow: _int = ...
tuple: _int = ...
prefix_map: _int = ...
coerce: _int = ...
cast: _int = ...
function: _int = ...
python: _int = ...
adapt: _int = ...
int: _int = ...
float: _int = ...
callable: _int = ...

class ComparisonMode(IntEnum):
none: int = ...
Expand Down
28 changes: 28 additions & 0 deletions traits-stubs/traits-stubs/ctraits.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# (C) Copyright 2020-2022 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!

from typing import Any

# Constants used in DefaultValue enumeration.
_CALLABLE_AND_ARGS_DEFAULT_VALUE: int
_CALLABLE_DEFAULT_VALUE: int
_CONSTANT_DEFAULT_VALUE: int
_DICT_COPY_DEFAULT_VALUE: int
_DISALLOW_DEFAULT_VALUE: int
_LIST_COPY_DEFAULT_VALUE: int
_MAXIMUM_DEFAULT_VALUE_TYPE: int
_MISSING_DEFAULT_VALUE: int
_OBJECT_DEFAULT_VALUE: int
_TRAIT_DICT_OBJECT_DEFAULT_VALUE: int
_TRAIT_LIST_OBJECT_DEFAULT_VALUE: int
_TRAIT_SET_OBJECT_DEFAULT_VALUE: int

def _validate_complex_number(value: Any) -> complex: ...
def _validate_float(value: Any) -> float: ...

0 comments on commit 9f5fe4d

Please sign in to comment.