Infinidat / munch

A Munch is a Python dictionary that provides attribute-style access (a la JavaScript objects).
http://github.com/Infinidat/munch
MIT License
761 stars 84 forks source link

_make is part of namedtuple's contract, not tuple #72

Open marksteward opened 3 years ago

marksteward commented 3 years ago

I think these should read namedtuple:

https://github.com/Infinidat/munch/blob/d0aeb06/munch/__init__.py#L461 https://github.com/Infinidat/munch/blob/d0aeb06/munch/__init__.py#L523

Currently, if a custom class inherits from tuple and an instance ends up with a _make attribute, munch will call it when the __dict__ attribute is accessed. Unlikely to happen by chance, but munch also inserts itself into SafeLoader, so it should probably err on the defensive side.

marksteward commented 3 years ago

Apparently there's no easy way to detect namedtuples other than existence of these not-quite-magic methods. This feels like a footgun, but I'm not sure how to fix it now.

dangillet commented 2 years ago

From https://bugs.python.org/issue7796, it seems that the "easiest" way to check if we're dealing with a namedtuple is to check that the type is tuple and that it has a _fields attribute.

Also see this answer from A. Martelli on SO: https://stackoverflow.com/a/2166841