Skip to content

Commit

Permalink
Textualize#3662 remove hardcoded decimal point
Browse files Browse the repository at this point in the history
  • Loading branch information
butteredwaffles committed Nov 21, 2023
1 parent 896aa9f commit 0fc3079
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Loading indicators and app notifications overlapped in the wrong order https://github.com/Textualize/textual/issues/3677
- Widgets being loaded are disabled and have their scrolling explicitly disabled too https://github.com/Textualize/textual/issues/3677
- Method render on a widget could be called before mounting said widget https://github.com/Textualize/textual/issues/2914
- Input type number only accepting decimal point of locale-specific decimals https://github.com/Textualize/textual/issues/3662

### Added

Expand Down
3 changes: 2 additions & 1 deletion src/textual/widgets/_input.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import locale
import re
from dataclasses import dataclass
from typing import ClassVar, Iterable
Expand Down Expand Up @@ -32,7 +33,7 @@

_RESTRICT_TYPES = {
"integer": r"[-+]?\d*",
"number": r"[-+]?\d*\.?\d*[eE]?[-+]?\d*",
"number": rf"[-+]?\d*[\.{locale.localeconv().get('decimal_point')}]?\d*[eE]?[-+]?\d*",
"text": None,
}
InputType = Literal["integer", "number", "text"]
Expand Down
33 changes: 29 additions & 4 deletions tests/input/test_input_restrict.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import importlib
import locale
import re

import pytest

from textual.app import App, ComposeResult
from textual.widgets import Input
from textual.widgets._input import _RESTRICT_TYPES
from textual.widgets import Input, _input


def test_input_number_type():
"""Test number type regex."""
number = _RESTRICT_TYPES["number"]
locale._override_localeconv = {"decimal_point": "."}
importlib.reload(_input)
number = _input._RESTRICT_TYPES["number"]
assert re.fullmatch(number, "0")
assert re.fullmatch(number, "0.")
assert re.fullmatch(number, ".")
Expand All @@ -27,9 +30,31 @@ def test_input_number_type():
assert not re.fullmatch(number, "nan")


def test_input_number_type_non_period_decimal_point():
"""Test number type regex for locales with different decimal points."""
locale._override_localeconv = {"decimal_point": ","}
importlib.reload(_input)
number = _input._RESTRICT_TYPES["number"]
assert re.fullmatch(number, "0")
assert re.fullmatch(number, "0,")
assert re.fullmatch(number, ",")
assert re.fullmatch(number, ",0")
assert re.fullmatch(number, "1,1")
assert re.fullmatch(number, "1e1")
assert re.fullmatch(number, "1,2e")
assert re.fullmatch(number, "1,2e10")
assert re.fullmatch(number, "1,2E10")
assert re.fullmatch(number, "1,2e-")
assert re.fullmatch(number, "1,2e-10")
assert not re.fullmatch(number, "1,2e10e")
assert not re.fullmatch(number, "1f2")
assert not re.fullmatch(number, "inf")
assert not re.fullmatch(number, "nan")


def test_input_integer_type():
"""Test input type regex"""
integer = _RESTRICT_TYPES["integer"]
integer = _input._RESTRICT_TYPES["integer"]
assert re.fullmatch(integer, "0")
assert re.fullmatch(integer, "1")
assert re.fullmatch(integer, "10")
Expand Down

0 comments on commit 0fc3079

Please sign in to comment.