Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-144087: Add support for unicode MINUS SIGN in int, float and complex#144095
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
Closed
Uh oh!
There was an error while loading. Please reload this page.
Closed
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
8951088
Add support for unicode MINUS SIGN in `int`, `float` and `complex`
johnslavik d3f7c80
Add some test coverage
johnslavik 3fe1b82
Fix test
johnslavik 973e191
Add news entry
johnslavik 2695c06
Add C API test coverage (only `test_long_fromunicodeobject`)
johnslavik cf0324a
Use `:func:` in news entry for better display
johnslavik 3855919
Update `int` docs
johnslavik ae6cd6c
Update `float` docs
johnslavik 4a78b3a
Update `complex` docs
johnslavik f233ebd
Simplify complex `complex` docs
johnslavik 03d24b2
Fix formatting
johnslavik 5ce17c9
Update float syntax snippet
johnslavik b4b9574
Update stdtypes reference
johnslavik ff013f5
Clarify `complex` docs
johnslavik ca6eca0
Slightly changed "syntax" used in `float` production list
johnslavik 26dadc5
Fix alternative used in `complex` docs, I think it was incorrect
johnslavik 6391d27
Parenthesize consistently
johnslavik f7c3fa7
Parenthesize consistently vol. 2
johnslavik 0d519f4
Empty commit to try to skip incremental lint
johnslavik 6c5994a
Test NaNs properly
johnslavik 0e7110c
Fix trailing whitespace (was confused)
johnslavik 199a879
Parenthesize in news entry too
johnslavik a9874e7
Use `.. versionchanged:: next`
johnslavik 53c7e97
Add a *What's New* entry
johnslavik d349618
Merge branch 'main' to fix conflicts
johnslavik 8b4d9b1
Make `versionchanged` entry for `float` more fitting
johnslavik 4df638d
Add more tests
johnslavik f3dccd0
Correct the grammar reference
johnslavik 9353f09
Keep character in grammar reference
johnslavik 3c2e361
Adjust docs to not say "ASCII"
ambv 301eb4b
Remove more mentions of other signs being ASCII from docs
johnslavik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -55,16 +55,17 @@ def test_basic(self): | ||
| self.assertEqual(int(-3.5), -3) | ||
| self.assertEqual(int("-3"), -3) | ||
| self.assertEqual(int(" -3 "), -3) | ||
| self.assertEqual(int(" \N{MINUS SIGN}3 "), -3) | ||
| self.assertEqual(int("\N{EM SPACE}-3\N{EN SPACE}"), -3) | ||
| # Different base: | ||
| self.assertEqual(int("10",16), 16) | ||
| # Test conversion from strings and various anomalies | ||
| for s, v in L: | ||
| for sign in "", "+", "-": | ||
| for sign in "", "+", "-", "\N{MINUS SIGN}": | ||
| for prefix in "", " ", "\t", " \t\t ": | ||
| ss = prefix + sign + s | ||
| vv = v | ||
| if sign == "-" and v is not ValueError: | ||
| if sign in ("-", "\N{MINUS SIGN}") and v is not ValueError: | ||
| vv = -v | ||
| try: | ||
| self.assertEqual(int(ss), vv) | ||
| @@ -237,6 +238,8 @@ def test_invalid_signs(self): | ||
| int('-') | ||
| with self.assertRaises(ValueError): | ||
| int('- 1') | ||
| with self.assertRaises(ValueError): | ||
| int('\N{MINUS SIGN} 1') | ||
| with self.assertRaises(ValueError): | ||
| int('+ 1') | ||
| with self.assertRaises(ValueError): | ||
| @@ -247,6 +250,7 @@ def test_unicode(self): | ||
| self.assertEqual(int('١٢٣٤٥٦٧٨٩٠'), 1234567890) | ||
| self.assertEqual(int("१२३४५६७८९०1234567890", 0), 12345678901234567890) | ||
| self.assertEqual(int('١٢٣٤٥٦٧٨٩٠', 0), 1234567890) | ||
| self.assertEqual(int('\N{MINUS SIGN}١٢٣٤٥٦٧٨٩٠', 0), -1234567890) | ||
| def test_underscores(self): | ||
| for lit in VALID_UNDERSCORE_LITERALS: | ||
| @@ -558,6 +562,7 @@ def test_max_str_digits_edge_cases(self): | ||
| int_class('1' * maxdigits + ' ') | ||
| int_class('+' + '1' * maxdigits) | ||
| int_class('-' + '1' * maxdigits) | ||
| int_class('\N{MINUS SIGN}' + '1' * maxdigits) | ||
| self.assertEqual(len(str(10 ** (maxdigits - 1))), maxdigits) | ||
| def check(self, i, base=None): | ||
| @@ -575,6 +580,7 @@ def test_max_str_digits(self): | ||
| self.check('1' * (maxdigits + 1) + ' ') | ||
| self.check('+' + '1' * (maxdigits + 1)) | ||
| self.check('-' + '1' * (maxdigits + 1)) | ||
| self.check('\N{MINUS SIGN}' + '1' * (maxdigits + 1)) | ||
| self.check('1' * (maxdigits + 1)) | ||
| i = 10 ** maxdigits | ||
| @@ -688,6 +694,8 @@ def test_sign_not_counted(self): | ||
| assert i == pos_i | ||
| neg_i = int_class(f'-{s}') | ||
| assert -pos_i == neg_i | ||
| neg_i = int_class(f'\N{MINUS SIGN}{s}') | ||
| assert -pos_i == neg_i | ||
johnslavik marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| str(pos_i) | ||
| str(neg_i) | ||
| @@ -799,6 +807,8 @@ def test_pylong_str_to_int(self): | ||
| assert -v1 == v3 | ||
| v4 = int(' +' + s + ' ') | ||
| assert v1 == v4 | ||
| v5 = int(' \N{MINUS SIGN}' + s) | ||
| assert -v1 == v5 | ||
| with self.assertRaises(ValueError) as err: | ||
| int(s + 'z') | ||
| with self.assertRaises(ValueError) as err: | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3 Misc/NEWS.d/next/Core_and_Builtins/2026-02-14-03-03-07.gh-issue-144087.KT9kaM.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| :func:`int`, :func:`float` and :func:`complex` now support strings | ||
| with ``−`` (Unicode minus sign, U+2212) as an alternative to ``-`` | ||
| (ASCII hyphen minus, U+002D). Contributed by Bartosz Sławecki. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.