[issue45054] json module should issue warning about duplicate keys

2021-11-13 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
assignee:  -> bob.ippolito

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45054] json module should issue warning about duplicate keys

2021-11-13 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

-0 on doing this. The suggested warning/error adds overhead that everyone would 
pay for but would almost never be of benefit.  I haven't seen this particular 
problem arise in practice.  The likely reasons it doesn't come up are 1) that 
generated data doesn't normally produce mixed type keys, 2) because mixed type 
keys don't round-trip, and 3) even using numeric keys only (not mixed) is 
uncommon because it results in poor outcomes that fail round-trip invariants.

Andrei Kulakov is right in saying that such data suggests deeper problems with 
the design and that static typing would be beneficial.

One last thought:  Even with regular dicts, we don't normally warn about 
encountering duplicate keys:

>>> dict([(1, 'run'), (1, 'zoo'), (3, 'tree')])
{1: 'zoo', 3: 'tree'}

--
nosy: +rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45054] json module should issue warning about duplicate keys

2021-11-11 Thread Andrei Kulakov


Andrei Kulakov  added the comment:

Another good option would be to use typed dict like `mydict : dict[int,str] = 
{}`; and use typed values when populating the dict; this way a type checker 
will warn you of inconsistent key types.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45054] json module should issue warning about duplicate keys

2021-11-11 Thread Andrei Kulakov


Andrei Kulakov  added the comment:

In general this sounds reasonable; - but a couple of thoughts / comments:

- If you have a dict with mixed numbers in str format and in number format 
(i.e. ints as numbers and ints as strings in your case), you are creating 
problems in many potential places. The core of the problem is logically 
inconsistent keys rather than the step of conversion to JSON. So the most 
useful place for warning would be when adding a new key, but that wouldn't be 
practical.

- Even if something is to be done at conversion to JSON, it's not clear if it 
should be a warning (would that be enough when the conversion is a logical 
bug?), or it should be some kind of strict=True mode that raises a ValueError?

--
nosy: +andrei.avk

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45054] json module should issue warning about duplicate keys

2021-08-31 Thread Kevin Mills


Kevin Mills  added the comment:

Sorry to the people I'm pinging, but I just noticed the initial dictionary in 
my example code is wrong. I figured I should fix it before anybody tested it 
and got confused about it not matching up with my description of the results.

It should've been:

import json
d1 = {"1": "fromstring", 1: "fromnumber"}
string = json.dumps(d1)
print(string)
d2 = json.loads(string)
print(d2)

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45054] json module should issue warning about duplicate keys

2021-08-30 Thread Dong-hee Na


Change by Dong-hee Na :


--
nosy: +corona10

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45054] json module should issue warning about duplicate keys

2021-08-30 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
nosy: +bob.ippolito

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45054] json module should issue warning about duplicate keys

2021-08-30 Thread Kevin Mills


New submission from Kevin Mills :

The json module will allow the following without complaint:

import json
d1 = {1: "fromstring", "1": "fromnumber"}
string = json.dumps(d1)
print(string)
d2 = json.loads(string)
print(d2)

And it prints:

{"1": "fromstring", "1": "fromnumber"}
{'1': 'fromnumber'}

This would be extremely confusing to anyone who doesn't already know that JSON 
keys have to be strings. Not only does `d1 != d2` (which the documentation does 
mention as a possibility after a round trip through JSON), but `len(d1) != 
len(d2)` and `d1['1'] != d2['1']`, even though '1' is in both.

I suggest that if json.dump or json.dumps notices that it is producing a JSON 
document with duplicate keys, it should issue a warning. Similarly, if 
json.load or json.loads notices that it is reading a JSON document with 
duplicate keys, it should also issue a warning.

--
components: Library (Lib)
messages: 400678
nosy: Zeturic
priority: normal
severity: normal
status: open
title: json module should issue warning about duplicate keys
type: enhancement
versions: Python 3.11

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com