espressomd / espresso

The ESPResSo package
https://espressomd.org
GNU General Public License v3.0
226 stars 183 forks source link

system.part.by_id() accepts bool arguments #4990

Open pm-blanco opened 4 weeks ago

pm-blanco commented 4 weeks ago

While debugging a code I found an interesting behavior in espresso. It seems that system.part.by_id() accepts bool variables as input arguments:

import espressomd
system = espressomd.System(box_l=[2]*3)
part = system.part.by_id(False)
print(part.id)
>>> Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "particle_data.pyx", line 107, in espressomd.particle_data.ParticleHandle.id.__get__
  File "particle_data.pyx", line 55, in espressomd.particle_data.ParticleHandle.update_particle_data
RuntimeError: Particle node for id 0 not found!

In this situation, I would intuitively expect to get a TypeError message but instead it seems that espresso automatically transforms my bool argument to 0. In a way, it is kind of expected because False in Python is used as a keyword for 0 but I can imagine that it could become a source of bugs for people with more basic knowledge of Python. I let you to decide if this a bug or a feature of espresso, although I think that it would be generally better to restrict the accepted variable types to avoid unexpected behaviors.