Skip to content

Forbid float as key/index #861

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
orsinium opened this issue Oct 9, 2019 · 6 comments
Closed

Forbid float as key/index #861

orsinium opened this issue Oct 9, 2019 · 6 comments
Labels
Hacktoberfest Hactoberfest fun! help wanted Extra attention is needed level:starter Good for newcomers rule request Adding a new rule
Milestone

Comments

@orsinium
Copy link
Collaborator

orsinium commented Oct 9, 2019

Rule request

Thesis

For lists/tuples float as key produces TypeError:

a = [1,2,3]
a[1] # 2
a[1.] # TypeError: list indices must be integers or slices, not float

For dicts float is a bad idea because of accuracy:

b = {.3: 3}
b[.3] # 3
b[.1 + .2] # KeyError: 0.30000000000000004

So, let's catch simple cases:

a[2.0]
a[b / c]  # use a[b // c] instead

Reasoning

The motivation is the same as in #739. Let's detect as many runtime errors statically as possible.

@orsinium orsinium added the rule request Adding a new rule label Oct 9, 2019
@sobolevn
Copy link
Member

sobolevn commented Oct 9, 2019

Well, I would say that this is not actually a type error. That's a best practice: don't use floats as key, it will hurt you. So, I am all for it!

@sobolevn sobolevn added Hacktoberfest Hactoberfest fun! help wanted Extra attention is needed level:starter Good for newcomers labels Oct 9, 2019
@sobolevn sobolevn added this to the Version 0.13 milestone Oct 9, 2019
@orsinium
Copy link
Collaborator Author

orsinium commented Oct 9, 2019

In the case of sequences, it's TypeError. In the case of maps, it's dangerous. In both cases, it's bad after all.

@sobolevn
Copy link
Member

sobolevn commented Oct 9, 2019

I guess we should forbid setting and getting float keys. That's what should be checked:

some_map = {0.1: 'a'}  # violation!

@ghost
Copy link

ghost commented Oct 13, 2019

I can try out this

@sobolevn
Copy link
Member

You are welcome! @abyss143

@sobolevn sobolevn assigned ghost Oct 13, 2019
@sobolevn sobolevn unassigned ghost Nov 12, 2019
@sobolevn
Copy link
Member

The only difference in the implementation is that we don't eval +, -, etc. Yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Hacktoberfest Hactoberfest fun! help wanted Extra attention is needed level:starter Good for newcomers rule request Adding a new rule
Projects
None yet
Development

No branches or pull requests

2 participants