first commit

This commit is contained in:
Ayxan
2022-05-23 00:16:32 +04:00
commit d660f2a4ca
24786 changed files with 4428337 additions and 0 deletions

View File

@ -0,0 +1,114 @@
# From scikit-image: https://github.com/scikit-image/scikit-image/blob/c2f8c4ab123ebe5f7b827bc495625a32bb225c10/skimage/_shared/_warnings.py
# Licensed under modified BSD license
__all__ = ["all_warnings", "expected_warnings"]
import inspect
import os
import re
import sys
import warnings
from contextlib import contextmanager
from unittest import mock
@contextmanager
def all_warnings():
"""
Context for use in testing to ensure that all warnings are raised.
Examples
--------
>>> import warnings
>>> def foo():
... warnings.warn(RuntimeWarning("bar"))
We raise the warning once, while the warning filter is set to "once".
Hereafter, the warning is invisible, even with custom filters:
>>> with warnings.catch_warnings():
... warnings.simplefilter('once')
... foo()
We can now run ``foo()`` without a warning being raised:
>>> from numpy.testing import assert_warns # doctest: +SKIP
>>> foo() # doctest: +SKIP
To catch the warning, we call in the help of ``all_warnings``:
>>> with all_warnings(): # doctest: +SKIP
... assert_warns(RuntimeWarning, foo)
"""
# Whenever a warning is triggered, Python adds a __warningregistry__
# member to the *calling* module. The exercize here is to find
# and eradicate all those breadcrumbs that were left lying around.
#
# We proceed by first searching all parent calling frames and explicitly
# clearing their warning registries (necessary for the doctests above to
# pass). Then, we search for all submodules of skimage and clear theirs
# as well (necessary for the skimage test suite to pass).
frame = inspect.currentframe()
if frame:
for f in inspect.getouterframes(frame):
f[0].f_locals["__warningregistry__"] = {}
del frame
for _, mod in list(sys.modules.items()):
try:
mod.__warningregistry__.clear()
except AttributeError:
pass
with warnings.catch_warnings(record=True) as w, mock.patch.dict(
os.environ, {"TRAITLETS_ALL_DEPRECATIONS": "1"}
):
warnings.simplefilter("always")
yield w
@contextmanager
def expected_warnings(matching):
r"""Context for use in testing to catch known warnings matching regexes
Parameters
----------
matching : list of strings or compiled regexes
Regexes for the desired warning to catch
Examples
--------
>>> from skimage import data, img_as_ubyte, img_as_float # doctest: +SKIP
>>> with expected_warnings(["precision loss"]): # doctest: +SKIP
... d = img_as_ubyte(img_as_float(data.coins())) # doctest: +SKIP
Notes
-----
Uses `all_warnings` to ensure all warnings are raised.
Upon exiting, it checks the recorded warnings for the desired matching
pattern(s).
Raises a ValueError if any match was not found or an unexpected
warning was raised.
Allows for three types of behaviors: "and", "or", and "optional" matches.
This is done to accomodate different build enviroments or loop conditions
that may produce different warnings. The behaviors can be combined.
If you pass multiple patterns, you get an orderless "and", where all of the
warnings must be raised.
If you use the "|" operator in a pattern, you can catch one of several warnings.
Finally, you can use "|\A\Z" in a pattern to signify it as optional.
"""
with all_warnings() as w:
# enter context
yield w
# exited user context, check the recorded warnings
remaining = [m for m in matching if r"\A\Z" not in m.split("|")]
for warn in w:
found = False
for match in matching:
if re.search(match, str(warn.message)) is not None:
found = True
if match in remaining:
remaining.remove(match)
if not found:
raise ValueError("Unexpected warning: %s" % str(warn.message))
if len(remaining) > 0:
msg = "No warning raised matching:\n%s" % "\n".join(remaining)
raise ValueError(msg)

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,380 @@
# pylint: disable=missing-docstring, too-few-public-methods
"""
Test the trait-type ``UseEnum``.
"""
import enum
import unittest
from traitlets import CaselessStrEnum, Enum, FuzzyEnum, HasTraits, TraitError, UseEnum
# -----------------------------------------------------------------------------
# TEST SUPPORT:
# -----------------------------------------------------------------------------
class Color(enum.Enum):
red = 1
green = 2
blue = 3
yellow = 4
class OtherColor(enum.Enum):
red = 0
green = 1
class CSColor(enum.Enum):
red = 1
Green = 2
BLUE = 3
YeLLoW = 4
color_choices = "red Green BLUE YeLLoW".split()
# -----------------------------------------------------------------------------
# TESTSUITE:
# -----------------------------------------------------------------------------
class TestUseEnum(unittest.TestCase):
# pylint: disable=invalid-name
class Example(HasTraits):
color = UseEnum(Color, help="Color enum")
def test_assign_enum_value(self):
example = self.Example()
example.color = Color.green
self.assertEqual(example.color, Color.green)
def test_assign_all_enum_values(self):
# pylint: disable=no-member
enum_values = [value for value in Color.__members__.values()]
for value in enum_values:
self.assertIsInstance(value, Color)
example = self.Example()
example.color = value
self.assertEqual(example.color, value)
self.assertIsInstance(value, Color)
def test_assign_enum_value__with_other_enum_raises_error(self):
example = self.Example()
with self.assertRaises(TraitError):
example.color = OtherColor.green
def test_assign_enum_name_1(self):
# -- CONVERT: string => Enum value (item)
example = self.Example()
example.color = "red"
self.assertEqual(example.color, Color.red)
def test_assign_enum_value_name(self):
# -- CONVERT: string => Enum value (item)
# pylint: disable=no-member
enum_names = [enum_val.name for enum_val in Color.__members__.values()]
for value in enum_names:
self.assertIsInstance(value, str)
example = self.Example()
enum_value = Color.__members__.get(value)
example.color = value
self.assertIs(example.color, enum_value)
self.assertEqual(example.color.name, value)
def test_assign_scoped_enum_value_name(self):
# -- CONVERT: string => Enum value (item)
scoped_names = ["Color.red", "Color.green", "Color.blue", "Color.yellow"]
for value in scoped_names:
example = self.Example()
example.color = value
self.assertIsInstance(example.color, Color)
self.assertEqual(str(example.color), value)
def test_assign_bad_enum_value_name__raises_error(self):
# -- CONVERT: string => Enum value (item)
bad_enum_names = ["UNKNOWN_COLOR", "RED", "Green", "blue2"]
for value in bad_enum_names:
example = self.Example()
with self.assertRaises(TraitError):
example.color = value
def test_assign_enum_value_number_1(self):
# -- CONVERT: number => Enum value (item)
example = self.Example()
example.color = 1 # == Color.red.value
example.color = Color.red.value
self.assertEqual(example.color, Color.red)
def test_assign_enum_value_number(self):
# -- CONVERT: number => Enum value (item)
# pylint: disable=no-member
enum_numbers = [enum_val.value for enum_val in Color.__members__.values()]
for value in enum_numbers:
self.assertIsInstance(value, int)
example = self.Example()
example.color = value
self.assertIsInstance(example.color, Color)
self.assertEqual(example.color.value, value)
def test_assign_bad_enum_value_number__raises_error(self):
# -- CONVERT: number => Enum value (item)
bad_numbers = [-1, 0, 5]
for value in bad_numbers:
self.assertIsInstance(value, int)
assert UseEnum(Color).select_by_number(value, None) is None
example = self.Example()
with self.assertRaises(TraitError):
example.color = value
def test_ctor_without_default_value(self):
# -- IMPLICIT: default_value = Color.red (first enum-value)
class Example2(HasTraits):
color = UseEnum(Color)
example = Example2()
self.assertEqual(example.color, Color.red)
def test_ctor_with_default_value_as_enum_value(self):
# -- CONVERT: number => Enum value (item)
class Example2(HasTraits):
color = UseEnum(Color, default_value=Color.green)
example = Example2()
self.assertEqual(example.color, Color.green)
def test_ctor_with_default_value_none_and_not_allow_none(self):
# -- IMPLICIT: default_value = Color.red (first enum-value)
class Example2(HasTraits):
color1 = UseEnum(Color, default_value=None, allow_none=False)
color2 = UseEnum(Color, default_value=None)
example = Example2()
self.assertEqual(example.color1, Color.red)
self.assertEqual(example.color2, Color.red)
def test_ctor_with_default_value_none_and_allow_none(self):
class Example2(HasTraits):
color1 = UseEnum(Color, default_value=None, allow_none=True)
color2 = UseEnum(Color, allow_none=True)
example = Example2()
self.assertIs(example.color1, None)
self.assertIs(example.color2, None)
def test_assign_none_without_allow_none_resets_to_default_value(self):
class Example2(HasTraits):
color1 = UseEnum(Color, allow_none=False)
color2 = UseEnum(Color)
example = Example2()
example.color1 = None
example.color2 = None
self.assertIs(example.color1, Color.red)
self.assertIs(example.color2, Color.red)
def test_assign_none_to_enum_or_none(self):
class Example2(HasTraits):
color = UseEnum(Color, allow_none=True)
example = Example2()
example.color = None
self.assertIs(example.color, None)
def test_assign_bad_value_with_to_enum_or_none(self):
class Example2(HasTraits):
color = UseEnum(Color, allow_none=True)
example = Example2()
with self.assertRaises(TraitError):
example.color = "BAD_VALUE"
def test_info(self):
choices = color_choices
class Example(HasTraits):
enum1 = Enum(choices, allow_none=False)
enum2 = CaselessStrEnum(choices, allow_none=False)
enum3 = FuzzyEnum(choices, allow_none=False)
enum4 = UseEnum(CSColor, allow_none=False)
for i in range(1, 5):
attr = "enum%s" % i
enum = getattr(Example, attr)
enum.allow_none = True
info = enum.info()
self.assertEqual(len(info.split(", ")), len(choices), info.split(", "))
self.assertIn("or None", info)
info = enum.info_rst()
self.assertEqual(len(info.split("|")), len(choices), info.split("|"))
self.assertIn("or `None`", info)
# Check no single `\` exists.
self.assertNotRegex(info, r"\b\\\b")
enum.allow_none = False
info = enum.info()
self.assertEqual(len(info.split(", ")), len(choices), info.split(", "))
self.assertNotIn("None", info)
info = enum.info_rst()
self.assertEqual(len(info.split("|")), len(choices), info.split("|"))
self.assertNotIn("None", info)
# Check no single `\` exists.
self.assertNotRegex(info, r"\b\\\b")
# -----------------------------------------------------------------------------
# TESTSUITE:
# -----------------------------------------------------------------------------
class TestFuzzyEnum(unittest.TestCase):
# Check mostly `validate()`, Ctor must be checked on generic `Enum`
# or `CaselessStrEnum`.
def test_search_all_prefixes__overwrite(self):
class FuzzyExample(HasTraits):
color = FuzzyEnum(color_choices, help="Color enum")
example = FuzzyExample()
for color in color_choices:
for wlen in range(1, len(color)):
value = color[:wlen]
example.color = value
self.assertEqual(example.color, color)
example.color = value.upper()
self.assertEqual(example.color, color)
example.color = value.lower()
self.assertEqual(example.color, color)
def test_search_all_prefixes__ctor(self):
class FuzzyExample(HasTraits):
color = FuzzyEnum(color_choices, help="Color enum")
for color in color_choices:
for wlen in range(1, len(color)):
value = color[:wlen]
example = FuzzyExample()
example.color = value
self.assertEqual(example.color, color)
example = FuzzyExample()
example.color = value.upper()
self.assertEqual(example.color, color)
example = FuzzyExample()
example.color = value.lower()
self.assertEqual(example.color, color)
def test_search_substrings__overwrite(self):
class FuzzyExample(HasTraits):
color = FuzzyEnum(color_choices, help="Color enum", substring_matching=True)
example = FuzzyExample()
for color in color_choices:
for wlen in range(0, 2):
value = color[wlen:]
example.color = value
self.assertEqual(example.color, color)
example.color = value.upper()
self.assertEqual(example.color, color)
example.color = value.lower()
self.assertEqual(example.color, color)
def test_search_substrings__ctor(self):
class FuzzyExample(HasTraits):
color = FuzzyEnum(color_choices, help="Color enum", substring_matching=True)
color = color_choices[-1] # 'YeLLoW'
for end in (-1, len(color)):
for start in range(1, len(color) - 2):
value = color[start:end]
example = FuzzyExample()
example.color = value
self.assertEqual(example.color, color)
example = FuzzyExample()
example.color = value.upper()
self.assertEqual(example.color, color)
def test_assign_other_raises(self):
def new_trait_class(case_sensitive, substring_matching):
class Example(HasTraits):
color = FuzzyEnum(
color_choices,
case_sensitive=case_sensitive,
substring_matching=substring_matching,
)
return Example
example = new_trait_class(case_sensitive=False, substring_matching=False)()
with self.assertRaises(TraitError):
example.color = ""
with self.assertRaises(TraitError):
example.color = "BAD COLOR"
with self.assertRaises(TraitError):
example.color = "ed"
example = new_trait_class(case_sensitive=True, substring_matching=False)()
with self.assertRaises(TraitError):
example.color = ""
with self.assertRaises(TraitError):
example.color = "Red" # not 'red'
example = new_trait_class(case_sensitive=True, substring_matching=True)()
with self.assertRaises(TraitError):
example.color = ""
with self.assertRaises(TraitError):
example.color = "BAD COLOR"
with self.assertRaises(TraitError):
example.color = "green" # not 'Green'
with self.assertRaises(TraitError):
example.color = "lue" # not (b)'LUE'
with self.assertRaises(TraitError):
example.color = "lUE" # not (b)'LUE'
example = new_trait_class(case_sensitive=False, substring_matching=True)()
with self.assertRaises(TraitError):
example.color = ""
with self.assertRaises(TraitError):
example.color = "BAD COLOR"
def test_ctor_with_default_value(self):
def new_trait_class(default_value, case_sensitive, substring_matching):
class Example(HasTraits):
color = FuzzyEnum(
color_choices,
default_value=default_value,
case_sensitive=case_sensitive,
substring_matching=substring_matching,
)
return Example
for color in color_choices:
example = new_trait_class(color, False, False)()
self.assertEqual(example.color, color)
example = new_trait_class(color.upper(), False, False)()
self.assertEqual(example.color, color)
color = color_choices[-1] # 'YeLLoW'
example = new_trait_class(color, True, False)()
self.assertEqual(example.color, color)
# FIXME: default value not validated!
# with self.assertRaises(TraitError):
# example = new_trait_class(color.lower(), True, False)

View File

@ -0,0 +1,39 @@
import sys
from subprocess import PIPE, Popen
def get_output_error_code(cmd):
"""Get stdout, stderr, and exit code from running a command"""
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
out, err = p.communicate()
out = out.decode("utf8", "replace")
err = err.decode("utf8", "replace")
return out, err, p.returncode
def check_help_output(pkg, subcommand=None):
"""test that `python -m PKG [subcommand] -h` works"""
cmd = [sys.executable, "-m", pkg]
if subcommand:
cmd.extend(subcommand)
cmd.append("-h")
out, err, rc = get_output_error_code(cmd)
assert rc == 0, err
assert "Traceback" not in err
assert "Options" in out
assert "--help-all" in out
return out, err
def check_help_all_output(pkg, subcommand=None):
"""test that `python -m PKG --help-all` works"""
cmd = [sys.executable, "-m", pkg]
if subcommand:
cmd.extend(subcommand)
cmd.append("--help-all")
out, err, rc = get_output_error_code(cmd)
assert rc == 0, err
assert "Traceback" not in err
assert "Options" in out
assert "Class options" in out
return out, err