[issue31239] namedtuple comparison ignores types

2017-08-19 Thread R. David Murray

R. David Murray added the comment:

This is by design: namedtuples are tuples in which you can access the elements 
by name.  If you have a tuple with the same elements, but no name access, they 
should compare equal, because they are fundamentally tuples.  The names are 
just a convenience.

Even if this were not so, it is not something that could be changed, for 
backward compatibility reasons.  

If you want a different data types, make them :)

--
nosy: +r.david.murray
resolution:  -> rejected
stage:  -> resolved
status: open -> closed

___
Python tracker 

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



[issue31239] namedtuple comparison ignores types

2017-08-18 Thread Vlad Shcherbina

Vlad Shcherbina added the comment:

While we are at it, namedtuple inherits other operations that make no sense for 
fixed-length tuples:

>>> Rectangle(width=1, height=2) * 3
(1, 2, 1, 2, 1, 2)
>>> Rectangle(width=1, height=2) + Ellipse(x_axis=3, y_axis=4)
(1, 2, 3, 4)

But those are not so problematic because they are less likely to occur in 
practice.

--

___
Python tracker 

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



[issue31239] namedtuple comparison ignores types

2017-08-18 Thread Vlad Shcherbina

New submission from Vlad Shcherbina:

Toy example:

>>> from collections import namedtuple
>>> Rectangle = namedtuple('Rectangle', 'width height')
>>> Ellipse = namedtuple('Ellipse', 'x_axis y_axis')
>>> Rectangle(width=1, height=2) == Ellipse(x_axis=1, y_axis=2)
True

I understand this happens because namedtuple inherits comparisons and hash from 
the regular tuple.
However, this seems like confusing and dangerous behavior.
It is especially annoying to debug when these tuples are used as dict keys 
(repr() shows one thing and dict item access shows another).

Why would anyone mix named tuples of different types?
Here is a use case: typing.NamedTuple together with typing.Union would be a 
neat way to express algebraic data types.

Haskell's
data Shape = Rectangle Int Int | Ellipse Int Intderiving(Eq, Show)

would become
Shape = Union[Rectangle, Ellipse]

except that it does not work as expected because of the flawed comparisons.

--
components: Library (Lib)
messages: 300562
nosy: vlad
priority: normal
severity: normal
status: open
title: namedtuple comparison ignores types
type: behavior
versions: Python 2.7, Python 3.3, Python 3.4, Python 3.5, Python 3.6, Python 3.7

___
Python tracker 

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