protocolbuffers / protobuf

Protocol Buffers - Google's data interchange format
http://protobuf.dev
Other
65.2k stars 15.44k forks source link

python well_known_type: naming convention #6018

Closed jonathansp closed 5 years ago

jonathansp commented 5 years ago

What language does this apply to?

proto2/3 - python - well_known_types

Describe the problem you are trying to solve.

Most of the python codebase uses the _lowercase_withunderscores convention for methods and functions, as it is enforced by linters. The classes defined in _well_knowntypes.py don't follow this convention as they are similar to the definition from other languages. Although it's not an issue, I think we could create aliases for those methods in order to make then more idiomatic. Example from python/google/protobuf/internal/well_known_types.py:189


  def ToMicroseconds(self):
    """Converts Timestamp to microseconds since epoch."""
    return (self.seconds * _MICROS_PER_SECOND +
            self.nanos // _NANOS_PER_MICROSECOND)

They are fine, but if you put this call in a codebase which follows the lowercase code convention, it's not so good:

if user_profile.created_at.ToNanoseconds() == expected_created_at:

Also, it is a little inconsistent since well_known_types module uses this convention.

Describe the solution you'd like

As python protobuf is widely used, renaming the methods is not a solution. However, I can't see any issues in adding aliases to these methods.


  def ToMicroseconds(self):
    """Converts Timestamp to microseconds since epoch."""
    return (self.seconds * _MICROS_PER_SECOND +
            self.nanos // _NANOS_PER_MICROSECOND)

   to_microseconds = ToMicroseconds
...

Describe alternatives you've considered

Create an external extension/lib to fix them (I personally dislike it) or keep it as it is.

References:

anandolee commented 5 years ago

method_name is suggested but MethodName is also allowed for consistency in google. Most old code in protobuf python are using MethodName for example text_format.py message.py descriptor.py, so well_known_types.py also use MethodName for consistency