Skip to content

Declare globals FMT_XML and FMT_BINARY#13054

Closed
cclauss wants to merge 1 commit intopython:masterfrom
cclauss:patch-2
Closed

Declare globals FMT_XML and FMT_BINARY#13054
cclauss wants to merge 1 commit intopython:masterfrom
cclauss:patch-2

Conversation

@cclauss
Copy link
Copy Markdown
Contributor

@cclauss cclauss commented May 2, 2019

PlistFormat = enum.Enum('PlistFormat', 'FMT_XML FMT_BINARY', module=__name__)
globals().update(PlistFormat.__members__)
global FMT_XML, FMT_BINARY # explicitly declare globals to aid humans and linters

The first two lines create two global variables in a nonstandard way.
The third line makes explicit what the first two lines have accomplished.

flake8 testing of https://github.com/python/cpython on Python 3.7.1

$ flake8 . --count --select=E9,F63,F72,F82 --show-source --statistics

./Lib/plistlib.py:48:1: F822 undefined name 'FMT_BINARY' in __all__
__all__ = [
^
./Lib/plistlib.py:48:1: F822 undefined name 'FMT_XML' in __all__
__all__ = [
^
./Lib/plistlib.py:112:29: F821 undefined name 'FMT_XML'
        dump(value, fp, fmt=FMT_XML, sort_keys=True, skipkeys=False)
                            ^
./Lib/plistlib.py:135:24: F821 undefined name 'FMT_XML'
    dump(value, f, fmt=FMT_XML, sort_keys=True, skipkeys=False)
                       ^
./Lib/plistlib.py:917:5: F821 undefined name 'FMT_XML'
    FMT_XML: dict(
    ^
./Lib/plistlib.py:922:5: F821 undefined name 'FMT_BINARY'
    FMT_BINARY: dict(
    ^
./Lib/plistlib.py:961:28: F821 undefined name 'FMT_XML'
def dump(value, fp, *, fmt=FMT_XML, sort_keys=True, skipkeys=False):
                           ^
./Lib/plistlib.py:972:25: F821 undefined name 'FMT_XML'
def dumps(value, *, fmt=FMT_XML, skipkeys=False, sort_keys=True):
                        ^

E901,E999,F821,F822,F823 are the "showstopper" flake8 issues that can halt the runtime with a SyntaxError, NameError, etc. These 5 are different from most other flake8 issues which are merely "style violations" -- useful for readability but they do not effect runtime safety.

  • F821: undefined name name
  • F822: undefined name name in __all__
  • F823: local variable name referenced before assignment
  • E901: SyntaxError or IndentationError
  • E999: SyntaxError -- failed to compile a file into an Abstract Syntax Tree

> PlistFormat = enum.Enum('PlistFormat', 'FMT_XML FMT_BINARY', module=__name__)
> globals().update(PlistFormat.__members__)
> global FMT_XML, FMT_BINARY  # explicitly declare globals to aid humans and linters

The first two lines create two global variables in a nonstandard way.  The third line makes explicit what the first two lines have done.

[flake8](http://flake8.pycqa.org) testing of https://github.com/python/cpython on Python 3.7.1

$ __flake8 . --count --select=E9,F63,F72,F82 --show-source --statistics__
```
./Lib/plistlib.py:48:1: F822 undefined name 'FMT_BINARY' in __all__
__all__ = [
^
./Lib/plistlib.py:48:1: F822 undefined name 'FMT_XML' in __all__
__all__ = [
^
./Lib/plistlib.py:112:29: F821 undefined name 'FMT_XML'
        dump(value, fp, fmt=FMT_XML, sort_keys=True, skipkeys=False)
                            ^
./Lib/plistlib.py:135:24: F821 undefined name 'FMT_XML'
    dump(value, f, fmt=FMT_XML, sort_keys=True, skipkeys=False)
                       ^
./Lib/plistlib.py:917:5: F821 undefined name 'FMT_XML'
    FMT_XML: dict(
    ^
./Lib/plistlib.py:922:5: F821 undefined name 'FMT_BINARY'
    FMT_BINARY: dict(
    ^
./Lib/plistlib.py:961:28: F821 undefined name 'FMT_XML'
def dump(value, fp, *, fmt=FMT_XML, sort_keys=True, skipkeys=False):
                           ^
./Lib/plistlib.py:972:25: F821 undefined name 'FMT_XML'
def dumps(value, *, fmt=FMT_XML, skipkeys=False, sort_keys=True):
                        ^
```
__E901,E999,F821,F822,F823__ are the "_showstopper_" [flake8](http://flake8.pycqa.org) issues that can halt the runtime with a SyntaxError, NameError, etc. These 5 are different from most other flake8 issues which are merely "style violations" -- useful for readability but they do not effect runtime safety.
* F821: undefined name `name`
* F822: undefined name `name` in `__all__`
* F823: local variable name referenced before assignment
* E901: SyntaxError or IndentationError
* E999: SyntaxError -- failed to compile a file into an Abstract Syntax Tree
@tirkarthi
Copy link
Copy Markdown
Member

I am not sure of these changes to be made just for silence linting. There are places where this pattern is followed. I am adding module maintainers to take a call.

cc: @serhiy-storchaka, @ronaldoussoren

Lib/plistlib.py
69:globals().update(PlistFormat.__members__)

Lib/re.py
162:globals().update(RegexFlag.__members__)

Lib/sre_constants.py
72:    globals().update({item.name: item for item in items})

Lib/http/client.py
102:globals().update(http.HTTPStatus.__members__)

Lib/multiprocessing/__init__.py
23:globals().update((name, getattr(context._default_context, name)) for name in __all__)

@serhiy-storchaka
Copy link
Copy Markdown
Member

I concur with @tirkarthi.

@cclauss cclauss closed this May 3, 2019
@cclauss cclauss deleted the patch-2 branch May 3, 2019 11:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants