Skip to content

Python: don't crash on complex-number literals #364

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 1 commit into from
Aug 20, 2021

Conversation

jakelishman
Copy link
Contributor

This fixes #363: crashing on complex-number literals in Python 2 and 3. I modified the JSON serialiser to string-ify complex numbers, rather than passing them directly as numeric values. That's perhaps not 100% ideal, but it's the same as the behaviour of Ruby's to_json method. I've added a test that the behaviour works, as well.

Running codeclimate analyze on the same test file in issue #363 with the same settings (mass_threshold: 1) now produces the expected output in both Python 2 and Python 3:

Starting analysis
Running duplication: Done!

== complex.py (3 issues) ==
1-2: Similar blocks of code found in 3 locations. Consider refactoring. [duplication]
4-5: Similar blocks of code found in 3 locations. Consider refactoring. [duplication]
7-8: Similar blocks of code found in 3 locations. Consider refactoring. [duplication]

Analysis complete! Found 3 issues.

@jakelishman
Copy link
Contributor Author

Also: if this is added, it may cause projects to see a spike in detection numbers. Our package QuTiP would jump from 102 duplication issues to 190, for example.

Copy link

@hodgestar hodgestar left a comment

Choose a reason for hiding this comment

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

👍

Copy link
Contributor

@f-moya f-moya left a comment

Choose a reason for hiding this comment

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

Nice 👍🏼 , thanks for your contribution.

@f-moya f-moya force-pushed the fix-python-complex-numbers branch from 803fd80 to 5dd5eed Compare August 19, 2021 22:20
Complex literals cannot be serialised directly to JSON, but Python's AST
reports them as constant numeric values.  Ruby's `to_json` method
serialies complex numbers to their string representations, so we do
similar here.  An alternative would be to serialise to a two-element
list of the real and imaginary parts.
@f-moya f-moya force-pushed the fix-python-complex-numbers branch from 5dd5eed to 5b01aa3 Compare August 20, 2021 15:39
@jmcgill jmcgill merged commit 3945c66 into codeclimate:master Aug 20, 2021
@jakelishman jakelishman deleted the fix-python-complex-numbers branch March 4, 2022 22:48
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.

Complex-number literals raise JSON error in Python
4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy