2

Is there a list somewhere (or better yet, a module!) that I can use to check whether a string is a "bad" choice for a variable name, where "bad" is defined as something like "is a keyword or built-in function etc."?

I have a script that generates Python classes from a Jinja template (Django models to be precise) and I'd like to fix any field names that are unsuitable for reasons like the ones I mentioned above.

So far, I've got a check that looks like this:

def is_bad_name(name):
    return keyword.iskeyword(name) or (name in ["type"])

So another way of phrasing my question would be: what else should I put in that list along with "type" ?

I realize that there can't be any complete list since it will vary depending on what is defined in other modules I'm using, but I wonder if there is a good list of things that should pretty much never be used. Thanks!

John B
  • 3,391
  • 5
  • 33
  • 29
  • 2
    `__builtins__` is a good place to start. – Jean-François Fabre Feb 21 '17 at 22:15
  • 2
    To get list of reserved words see http://stackoverflow.com/questions/22864221/is-the-list-of-python-reserved-words-and-builtins-available-in-a-library – Paul Rooney Feb 21 '17 at 22:17
  • 2
    If you're even considering that a variable name might conflict with something built in, you're already giving your variables bad names.. make them descriptive – Sayse Feb 21 '17 at 22:17
  • 1
    I wonder if it would be valid to match against what you already are, the match against all modules you're importing, and then match against `dir(module)` (i.e. `import os \n dir(os)`) – searchengine27 Feb 21 '17 at 22:17

2 Answers2

7

You probably want to check against __builtins__ keys:

>>> __builtins__.keys()

['__name__',
 '__doc__',
 '__package__',
 '__loader__',
 '__spec__',
 '__build_class__',
 '__import__',
 'abs',
 'all',
 'any',
 'ascii',
 'bin',
 'callable',
 'chr',
 'compile',
 'delattr',
 'dir',
 'divmod',
 'eval',
 'exec',
 'format',
 'getattr',
 'globals',
 'hasattr',
 'hash',
 'hex',
 'id',
 'input',
 'isinstance',
 'issubclass',
 'iter',
 'len',
 'locals',
 'max',
 'min',
 'next',
 'oct',
 'ord',
 'pow',
 'print',
 'repr',
 'round',
 'setattr',
 'sorted',
 'sum',
 'vars',
 'None',
 'Ellipsis',
 'NotImplemented',
 'False',
 'True',
 'bool',
 'memoryview',
 'bytearray',
 'bytes',
 'classmethod',
 'complex',
 'dict',
 'enumerate',
 'filter',
 'float',
 'frozenset',
 'property',
 'int',
 'list',
 'map',
 'object',
 'range',
 'reversed',
 'set',
 'slice',
 'staticmethod',
 'str',
 'super',
 'tuple',
 'type',
 'zip',
 '__debug__',
 'BaseException',
 'Exception',
 'TypeError',
 'StopAsyncIteration',
 'StopIteration',
 'GeneratorExit',
 'SystemExit',
 'KeyboardInterrupt',
 'ImportError',
 'ModuleNotFoundError',
 'OSError',
 'EnvironmentError',
 'IOError',
 'WindowsError',
 'EOFError',
 'RuntimeError',
 'RecursionError',
 'NotImplementedError',
 'NameError',
 'UnboundLocalError',
 'AttributeError',
 'SyntaxError',
 'IndentationError',
 'TabError',
 'LookupError',
 'IndexError',
 'KeyError',
 'ValueError',
 'UnicodeError',
 'UnicodeEncodeError',
 'UnicodeDecodeError',
 'UnicodeTranslateError',
 'AssertionError',
 'ArithmeticError',
 'FloatingPointError',
 'OverflowError',
 'ZeroDivisionError',
 'SystemError',
 'ReferenceError',
 'BufferError',
 'MemoryError',
 'Warning',
 'UserWarning',
 'DeprecationWarning',
 'PendingDeprecationWarning',
 'SyntaxWarning',
 'RuntimeWarning',
 'FutureWarning',
 'ImportWarning',
 'UnicodeWarning',
 'BytesWarning',
 'ResourceWarning',
 'ConnectionError',
 'BlockingIOError',
 'BrokenPipeError',
 'ChildProcessError',
 'ConnectionAbortedError',
 'ConnectionRefusedError',
 'ConnectionResetError',
 'FileExistsError',
 'FileNotFoundError',
 'IsADirectoryError',
 'NotADirectoryError',
 'InterruptedError',
 'PermissionError',
 'ProcessLookupError',
 'TimeoutError',
 'open',
 'quit',
 'exit',
 'copyright',
 'credits',
 'license',
 'help',
 '_']
>>> pprint.pprint(list(a))
['__name__',
 '__doc__',
 '__package__',
 '__loader__',
 '__spec__',
 '__build_class__',
 '__import__',
 'abs',
 'all',
 'any',
 'ascii',
 'bin',
 'callable',
 'chr',
 'compile',
 'delattr',
 'dir',
 'divmod',
 'eval',
 'exec',
 'format',
 'getattr',
 'globals',
 'hasattr',
 'hash',
 'hex',
 'id',
 'input',
 'isinstance',
 'issubclass',
 'iter',
 'len',
 'locals',
 'max',
 'min',
 'next',
 'oct',
 'ord',
 'pow',
 'print',
 'repr',
 'round',
 'setattr',
 'sorted',
 'sum',
 'vars',
 'None',
 'Ellipsis',
 'NotImplemented',
 'False',
 'True',
 'bool',
 'memoryview',
 'bytearray',
 'bytes',
 'classmethod',
 'complex',
 'dict',
 'enumerate',
 'filter',
 'float',
 'frozenset',
 'property',
 'int',
 'list',
 'map',
 'object',
 'range',
 'reversed',
 'set',
 'slice',
 'staticmethod',
 'str',
 'super',
 'tuple',
 'type',
 'zip',
 '__debug__',
 'BaseException',
 'Exception',
 'TypeError',
 'StopAsyncIteration',
 'StopIteration',
 'GeneratorExit',
 'SystemExit',
 'KeyboardInterrupt',
 'ImportError',
 'ModuleNotFoundError',
 'OSError',
 'EnvironmentError',
 'IOError',
 'WindowsError',
 'EOFError',
 'RuntimeError',
 'RecursionError',
 'NotImplementedError',
 'NameError',
 'UnboundLocalError',
 'AttributeError',
 'SyntaxError',
 'IndentationError',
 'TabError',
 'LookupError',
 'IndexError',
 'KeyError',
 'ValueError',
 'UnicodeError',
 'UnicodeEncodeError',
 'UnicodeDecodeError',
 'UnicodeTranslateError',
 'AssertionError',
 'ArithmeticError',
 'FloatingPointError',
 'OverflowError',
 'ZeroDivisionError',
 'SystemError',
 'ReferenceError',
 'BufferError',
 'MemoryError',
 'Warning',
 'UserWarning',
 'DeprecationWarning',
 'PendingDeprecationWarning',
 'SyntaxWarning',
 'RuntimeWarning',
 'FutureWarning',
 'ImportWarning',
 'UnicodeWarning',
 'BytesWarning',
 'ResourceWarning',
 'ConnectionError',
 'BlockingIOError',
 'BrokenPipeError',
 'ChildProcessError',
 'ConnectionAbortedError',
 'ConnectionRefusedError',
 'ConnectionResetError',
 'FileExistsError',
 'FileNotFoundError',
 'IsADirectoryError',
 'NotADirectoryError',
 'InterruptedError',
 'PermissionError',
 'ProcessLookupError',
 'TimeoutError',
 'open',
 'quit',
 'exit',
 'copyright',
 'credits',
 'license',
 'help',
 '_']
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • SE doesn't soft wrap lines in blocks and on my 56" TV, a maximum of 85 characters can be displayed at once. Please consider hard wrapping at 80 chars for readability – CervEd Jun 10 '21 at 12:40
  • 1
    not that easy even with pretty printer; – Jean-François Fabre Jun 10 '21 at 13:03
  • yikes, that's a long list! thanks – CervEd Jun 10 '21 at 13:37
  • maybe that could be shortened with `...` in the middle. Just let people see the point. No need to copy the list, which depends on python version. – Jean-François Fabre Jun 10 '21 at 13:38
  • Yeah possibly, if it's something that is best generated dynamically since it depends on version, there's definitely a case for that. However, the list itself may be useful to humans and/or search engines. Idk – CervEd Jun 10 '21 at 13:41
2

You might want to add __builtins__.__dict__.keys() and sys.builtin_module_names to that list

karlson
  • 5,325
  • 3
  • 30
  • 62