marshmallow icon indicating copy to clipboard operation
marshmallow copied to clipboard

Remove unnecessary field aliases (`Str`, `Int`, `Bool`, etc.)

Open greyli opened this issue 3 years ago • 5 comments

Marshmallow/Flask-Marshmallow provides some aliases for fields, for example, Str and String. To keep everything simple and consistent, I suggest removing these aliases, so that the IDE will not prompt Url and URL when you type "U" (in that case the users have to make the choice each time).

Proposal:

Remove the following aliases:

  • Str
  • Int
  • Bool
  • Url
  • UrlFor
  • AbsoluteUrlFor

Keep the following:

  • String
  • Integer
  • Boolean
  • URL
  • URLFor
  • AbsoluteURLFor

Originally posted in https://github.com/greyli/apiflask/issues/63

greyli avatar Jun 12 '21 00:06 greyli

I'm unsure about this--I find the aliases to be rather convenient. Also, different projects use different conventions wrt abbreviations (e.g. URL vs Url), and it's kinda nice to support both.

sloria avatar Jun 19 '21 15:06 sloria

I don't mind such a breaking change in a major release. I don't find those aliases very useful and I like to have a single way to do things. For instance, those aliases allow a team to use both namings in the same code base, making grep more difficult.

This said, this argument and the one about code completion are kinda weak so I don't mind keeping things as they are either.

lafrech avatar Jun 28 '21 07:06 lafrech

There should be one-- and preferably only one --obvious way to do it.

PEP 20

There's a reason Python doesn't have string as an alias of str in builtins.

9999years avatar Jul 12 '21 16:07 9999years

In my IDE the aliases lose their type and docstring. The autocomplete plugin doesn't know how to infer this information, because it doesn't try to do any runtime evaluation. We could exploit that behavior to hide the aliases from it.

# fields.py

# Aliases
globals().update({
    'URL': Url,
    'Str': String,
    'Bool': Boolean,
    'Int': Integer,
})

deckar01 avatar Jul 12 '21 17:07 deckar01

Purposefully evading static analysis seems unwise.

9999years avatar Jul 12 '21 20:07 9999years