Skip to content
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

Fix mypy complaints about TraitError #1658

Merged
merged 3 commits into from
Jun 30, 2022
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
45 changes: 45 additions & 0 deletions traits-stubs/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,51 @@ Alternatively, some IDEs (including VS Code and PyCharm) can be configured to
perform type checking as you edit.


Development
-----------

To test traits stubs locally:

- Create a fresh venv and activate it, for example with::

$ python -m venv --clear ~/.venvs/traits-stubs && source ~/.venvs/traits-stubs/bin/activate

- Make sure all build-related packages are up to date

$ python -m pip install --upgrade pip setuptools wheel

- Install the Traits library into the environment (non-editable install)

$ python -m pip install .

- Install traits stubs in editable mode (from the repo, not from PyPI).

$ python -m pip install -e traits-stubs/

- Install mypy (or your favourite type checker)

$ python -m pip install mypy

- From the traits-stubs directory, run mypy on individual files in
traits_stubs_tests/examples with e.g.,

$ python -m mypy traits_stubs_tests/examples/completeness.py

- From the traits-stubs directory, run the test suite with:

$ python -m unittest discover -v traits_stubs_tests

Note: it's easy to get confusing results if you accidentally use the wrong
version of mypy. To avoid that, you can make sure that you *don't* have mypy
installed globally, and/or always invoke mypy using ``python -m mypy`` from
within the environment.

Note: the unittest run can give the wrong result in the presence of a stale
``.mypy_cache``. If you're getting a pass where you expect to get a failure
(or vice versa), try deleting the local cache and trying again.



Dependencies
------------

Expand Down
6 changes: 6 additions & 0 deletions traits-stubs/traits-stubs/api.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ from .constants import (
RICH_COMPARE as RICH_COMPARE,
)

from .trait_errors import (
TraitError as TraitError,
TraitNotificationError as TraitNotificationError,
DelegationError as DelegationError,
)

from .traits import (
Color as Color,
Default as Default,
Expand Down
26 changes: 26 additions & 0 deletions traits-stubs/traits_stubs_tests/examples/completeness.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# (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!

"""
A selection of code snippets used to test completeness of the stubs.
"""

from traits.api import HasStrictTraits, Str, TraitError


class HasName(HasStrictTraits):
name = Str()


def try_assigning_age(x: HasName, new_name: str):
try:
x.age = new_name
except TraitError:
raise ValueError(f"Bad age: {new_name}")