Skip to content
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
4 changes: 3 additions & 1 deletion tests/test_traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# also under the terms of the Modified BSD License.
from __future__ import annotations

import decimal
import pickle
import re
import typing as t
Expand Down Expand Up @@ -1391,7 +1392,7 @@ class TestLong(TraitTestBase):
obj = LongTrait()

_default_value = 99
_good_values = [10, -10]
_good_values = [10, -10, 10.0, decimal.Decimal("10.0")]
_bad_values = [
"ten",
[10],
Expand All @@ -1401,6 +1402,7 @@ class TestLong(TraitTestBase):
1j,
10.1,
-10.1,
decimal.Decimal("10.1"),
"10",
"-10",
"10L",
Expand Down
10 changes: 10 additions & 0 deletions traitlets/traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import contextlib
import enum
import inspect
import numbers
import os
import re
import sys
Expand Down Expand Up @@ -2646,6 +2647,15 @@ def __init__(
)

def validate(self, obj: t.Any, value: t.Any) -> G:
if not isinstance(value, int) and isinstance(value, numbers.Number):
# allow casting integer-valued numbers to int
# allows for more concise assignment like `4e9` which is a float
try:
int_value = int(value)
if int_value == value:
value = int_value
except Exception:
pass
if not isinstance(value, int):
self.error(obj, value)
return _validate_bounds(self, obj, value) # type:ignore[no-any-return]
Expand Down
Loading