Skip to content

'n' support for float format #4865

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

Merged
merged 14 commits into from
Apr 15, 2023
Merged

Conversation

JazzGlobal
Copy link
Contributor

@JazzGlobal JazzGlobal commented Apr 7, 2023

Contributes to #1656

I've only recently sprouted my Rustacean legs so let me know if there are better ways to achieve this. I'm sure there will be!

'{:n}'.format(999999.1234)
'{:n}'.format(9999.1234)
'{:n}'.format(1000000.1234)
'{:n}'.format(-1000000.1234)
'{:.8n}'.format(1000000.1234)
'{:.10n}'.format(1000000.1234)
'{:.11n}'.format(1000000.1234)
'{:.11n}'.format(-1000000.1234)
'{:0n}'.format(-1000000.1234)
'{:n}'.format(-1000000.1234)
'{:-1n}'.format(-1000000.1234)

'{:-1N}'.format(-1000000.1234)
'{:.1N}'.format(1000000.1234)
'{:0N}'.format(-1000000.1234)
'{:N}'.format(999999.1234)

yields

999999
9999.12
1e+06
-1e+06
1000000.1
1000000.123
1000000.1234
-1000000.1234
-1e+06
-1e+06
-1e+06
Unknown format code 'N' for object of type 'float'
Unknown format code 'N' for object of type 'float'
Unknown format code 'N' for object of type 'float'
Unknown format code 'N' for object of type 'float'

@JazzGlobal
Copy link
Contributor Author

This will probably need tests before it gets accepted, but I could not successfully run the tests in Lib/test/test_format.py ...

I used cargo run using the absolute path and also cargo run --release using the absolute path. Am I going about this incorrectly?

@fanninpm
Copy link
Contributor

fanninpm commented Apr 8, 2023

I believe you can use the following command to test your changes locally:

# this runs all of the tests (not necessary if you know which test is affected)
cargo run --release --features ssl,jit -- -m test -j 1 -u all --slowest --fail-env-changed -v
# this runs only the test suite named "test_whatever" (usually located at `Lib/test/test_whatever.py`)
cargo run --release --features ssl,jit -- -m test -j 1 -u all --slowest --fail-env-changed -v test_whatever

For a list of all resources and more command-line options, you can execute cargo run --release --features ssl,jit -- -m test -h.

@fanninpm
Copy link
Contributor

fanninpm commented Apr 8, 2023

To format the Rust code, try running cargo fmt --all.

@JazzGlobal
Copy link
Contributor Author

Something's not quite right with either the tests I added for the N case or the action. I'm not getting local test failures at least.

@JazzGlobal
Copy link
Contributor Author

Something's not quite right with either the tests I added for the N case or the action. I'm not getting local test failures at least.

I think I got everything figured out. Thanks for your help. A lot of my issues were because of my dev environment but I got them sorted out.

The issue with the test was caused by my assuming Python 3.11 supported the N float formatter code but it doesn't. Removed the functionality for that and the test should yield the same Error Py 3.11 does when N is used.

@JazzGlobal JazzGlobal changed the title N and n support for float format n support for float format Apr 9, 2023
@JazzGlobal JazzGlobal changed the title n support for float format 'n' support for float format Apr 9, 2023
@DimitrisJim
Copy link
Member

there's a passing test now in test_enum.py so 🎉 :

UNEXPECTED SUCCESS: test_format_enum_float (test.test_enum.TestEnum.test_format_enum_float)

there's also a panic in the formatting, looking at the runner logs it seems to be due to something being off during a slicing operation.

This test doesn't run on Windows (hence why you did not probably see this error). It blows up in test_locale of test_format and my guess would be format(1234.5, "n"). Try running that locally in the REPL and see what it does.

@JazzGlobal
Copy link
Contributor Author

there's a passing test now in test_enum.py so 🎉 :

UNEXPECTED SUCCESS: test_format_enum_float (test.test_enum.TestEnum.test_format_enum_float)

there's also a panic in the formatting, looking at the runner logs it seems to be due to something being off during a slicing operation.

This test doesn't run on Windows (hence why you did not probably see this error). It blows up in test_locale of test_format and my guess would be format(1234.5, "n"). Try running that locally in the REPL and see what it does.

Thank you for the feedback! I think I fixed it and I also unmarked the unexpected success

@youknowone
Copy link
Member

youknowone commented Apr 10, 2023

You also fixed more test

UNEXPECTED SUCCESS: test_locale (test.test_format.FormatTest.test_locale)

and another failure

======================================================================
FAIL: test_int__format__ (test.test_types.TypesTests.test_int__format__)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/work/RustPython/RustPython/pylib/Lib/test/test_types.py", line 373, in test_int__format__
    self.assertRaises(ValueError, 0 .__format__, format_spec)
AssertionError: ValueError not raised by __format__

@JazzGlobal
Copy link
Contributor Author

You also fixed more test

UNEXPECTED SUCCESS: test_locale (test.test_format.FormatTest.test_locale)

and another failure

======================================================================
FAIL: test_int__format__ (test.test_types.TypesTests.test_int__format__)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/work/RustPython/RustPython/pylib/Lib/test/test_types.py", line 373, in test_int__format__
    self.assertRaises(ValueError, 0 .__format__, format_spec)
AssertionError: ValueError not raised by __format__

Sorry I keep missing things. Can't run the tests on my dev environment so I have a wonky setup at the moment. Think I got them sorted out.

I allowed the N case to function for int formatting functionality when that wasn't supposed to happen. I did not want to introduce "Case" to the Number enum but I wanted the error messages to line up properly with Python 3.11. Hope this is okay

@DimitrisJim
Copy link
Member

Can't run the tests on my dev environment

Generally good idea to push here and wait for results since other platforms might have issues you can't catch, you only need to be able to parse the logs (which is straight-forward after doing it once or twice but can get slightly tedious).

@JazzGlobal
Copy link
Contributor Author

Can't run the tests on my dev environment

Generally good idea to push here and wait for results since other platforms might have issues you can't catch, you only need to be able to parse the logs (which is straight-forward after doing it once or twice but can get slightly tedious).

Glad to hear that, hopefully I won't hit your GitHub action minute limit haha. Can't recall if there's only limitations in private repos or not.

Forgot to run cargo fmt so the workflow will have to run at least once more.

Copy link
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for contributing!

@youknowone youknowone merged commit d918f7e into RustPython:main Apr 15, 2023
@youknowone
Copy link
Member

@JazzGlobal If you are interested in supporting locale, you can start with #4609

@JazzGlobal
Copy link
Contributor Author

@JazzGlobal If you are interested in supporting locale, you can start with #4609

Thank you, and thank you all for guiding me through my first Rust contribution 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants