Use class-based API instead of functions for enums

This commit is contained in:
Yegor 2020-10-09 20:04:49 +03:00
parent aaddba76ac
commit b2dd22c5e9
13 changed files with 154 additions and 52 deletions

View File

@ -100,13 +100,13 @@ class UnsupportedOperationError(WebTabError):
"""Raised when an operation is not supported with the given backend."""
TerminationStatus = enum.Enum('TerminationStatus', [
'normal',
'abnormal', # non-zero exit status
'crashed', # e.g. segfault
'killed',
'unknown',
])
class TerminationStatus(enum.Enum):
normal = 1
abnormal = 2 # non-zero exit status
crashed = 3 # e.g. segfault
killed = 4
unknown = 5
@attr.s

View File

@ -42,10 +42,24 @@ if typing.TYPE_CHECKING:
from qutebrowser.browser import browsertab
Target = enum.Enum('Target', ['normal', 'current', 'tab', 'tab_fg', 'tab_bg',
'window', 'yank', 'yank_primary', 'run', 'fill',
'hover', 'download', 'userscript', 'spawn',
'delete', 'right_click'])
class Target(enum.Enum):
normal = 1
current = 2
tab = 3
tab_fg = 4
tab_bg = 5
window = 6
yank = 7
yank_primary = 8
run = 9
fill = 10
hover = 11
download = 12
userscript = 13
spawn = 14
delete = 15
right_click = 16
class HintingError(Exception):

View File

@ -40,7 +40,13 @@ if typing.TYPE_CHECKING:
STARTCHARS = ":/?"
LastPress = enum.Enum('LastPress', ['none', 'filtertext', 'keystring'])
class LastPress(enum.Enum):
none = 1
filtertext = 2
keystring = 3
class CommandKeyParser(basekeyparser.BaseKeyParser):

View File

@ -49,7 +49,12 @@ class ColorFlags:
passthrough: If we're currently in passthrough-mode.
"""
CaretMode = enum.Enum('CaretMode', ['off', 'on', 'selection'])
class CaretMode(enum.Enum):
off = 1
on = 2
selection = 3
prompt = attr.ib(False)
insert = attr.ib(False)
command = attr.ib(False)

View File

@ -39,7 +39,10 @@ class Text(textbase.TextBase):
available. If not, the permanent text is shown.
"""
Text = enum.Enum('Text', ['normal', 'temp'])
class Text(enum.Enum):
normal = 1
temp = 2
def __init__(self, parent=None):
super().__init__(parent)

View File

@ -29,9 +29,16 @@ from qutebrowser.config import stylesheet
from qutebrowser.utils import usertypes, urlutils
# Note this has entries for success/error/warn from widgets.webview:LoadStatus
UrlType = enum.Enum('UrlType', ['success', 'success_https', 'error', 'warn',
'hover', 'normal'])
class UrlType(enum.Enum):
"""Note this has entries for success/error/warn from widgets.webview:LoadStatus."""
success = 1
success_https = 2
error = 3
warn = 4
hover = 5
normal = 6
class UrlText(textbase.TextBase):

View File

@ -77,8 +77,14 @@ class DocstringParser:
arg_descs: A dict of argument names to their descriptions
"""
State = enum.Enum('State', ['short', 'desc', 'desc_hidden',
'arg_start', 'arg_inside', 'misc'])
class State(enum.Enum):
short = 1
desc = 2
desc_hidden = 3
arg_start = 4
arg_inside = 5
misc = 6
def __init__(self, func: typing.Callable) -> None:
"""Constructor.

View File

@ -60,7 +60,10 @@ class NeighborList(typing.Sequence[_T]):
_mode: The current mode.
"""
Modes = enum.Enum('Modes', ['edge', 'exception'])
class Modes(enum.Enum):
edge = 1
exception = 2
def __init__(self, items: typing.Sequence[_T] = None,
default: typing.Union[_T, Unset] = UNSET,
@ -224,9 +227,15 @@ class NeighborList(typing.Sequence[_T]):
return self.curitem()
# The mode of a Question.
PromptMode = enum.Enum('PromptMode', ['yesno', 'text', 'user_pwd', 'alert',
'download'])
class PromptMode(enum.Enum):
"""The mode of a Question."""
yesno = 1
text = 2
user_pwd = 3
alert = 4
download = 5
class ClickTarget(enum.Enum):
@ -273,13 +282,24 @@ class Exit(enum.IntEnum):
err_init = 4
# Load status of a tab
LoadStatus = enum.Enum('LoadStatus', ['none', 'success', 'success_https',
'error', 'warn', 'loading'])
class LoadStatus(enum.Enum):
"""Load status of a tab."""
none = 1
success = 2
success_https = 3
error = 4
warn = 5
loading = 6
# Backend of a tab
Backend = enum.Enum('Backend', ['QtWebKit', 'QtWebEngine'])
class Backend(enum.Enum):
"""Backend of a tab."""
QtWebKit = 1
QtWebEngine = 2
class JsWorld(enum.Enum):
@ -292,15 +312,32 @@ class JsWorld(enum.Enum):
jseval = 4 #: World used for the jseval-command.
# Log level of a JS message. This needs to match up with the keys allowed for
# the content.javascript.log setting.
JsLogLevel = enum.Enum('JsLogLevel', ['unknown', 'info', 'warning', 'error'])
class JsLogLevel(enum.Enum):
"""Log level of a JS message.
This needs to match up with the keys allowed for the
content.javascript.log setting.
"""
unknown = 1
info = 2
warning = 3
error = 4
MessageLevel = enum.Enum('MessageLevel', ['error', 'warning', 'info'])
class MessageLevel(enum.Enum):
error = 1
warning = 2
info = 3
IgnoreCase = enum.Enum('IgnoreCase', ['smart', 'never', 'always'])
class IgnoreCase(enum.Enum):
smart = 1
never = 2
always = 3
class CommandValue(enum.Enum):
@ -458,16 +495,16 @@ class NavigationRequest:
"""A request to navigate to the given URL."""
Type = enum.Enum('Type', [
'link_clicked',
'typed', # QtWebEngine only
'form_submitted',
'form_resubmitted', # QtWebKit only
'back_forward',
'reloaded',
'redirect', # QtWebEngine >= 5.14 only
'other'
])
class Type(enum.Enum):
link_clicked = 1
typed = 2 # QtWebEngine only
form_submitted = 3
form_resubmitted = 4 # QtWebKit only
back_forward = 5
reloaded = 6
redirect = 7 # QtWebEngine >= 5.14 only
other = 8
url = attr.ib() # type: QUrl
navigation_type = attr.ib() # type: Type

View File

@ -89,10 +89,21 @@ class DistributionInfo:
pastebin_url = None
Distribution = enum.Enum(
'Distribution', ['unknown', 'ubuntu', 'debian', 'void', 'arch',
'gentoo', 'fedora', 'opensuse', 'linuxmint', 'manjaro',
'kde_flatpak'])
class Distribution(enum.Enum):
unknown = 1
ubuntu = 2
debian = 3
void = 4
arch = 5
gentoo = 6
fedora = 7
opensuse = 8
linuxmint = 9
manjaro = 10
kde_flatpak = 11
def distribution() -> typing.Optional[DistributionInfo]:

View File

@ -53,7 +53,10 @@ class Message:
print(self.text)
MsgType = enum.Enum('MsgType', 'insufficient_coverage, perfect_file')
class MsgType(enum.Enum):
insufficient_coverage = 1
perfect_file = 2
# A list of (test_file, tested_file) tuples. test_file can be None.

View File

@ -290,7 +290,10 @@ class TestRegister:
else:
assert pos_args == [('arg', 'arg')]
Enum = enum.Enum('Test', ['x', 'y'])
class Enum(enum.Enum):
x = 1
y = 2
@pytest.mark.parametrize('typ, inp, choices, expected', [
(int, '42', None, 42),

View File

@ -28,7 +28,10 @@ from PyQt5.QtCore import QUrl
from qutebrowser.commands import argparser, cmdexc
Enum = enum.Enum('Enum', ['foo', 'foo_bar'])
class Enum(enum.Enum):
foo = 1
foo_bar = 2
class TestArgumentParser:

View File

@ -561,8 +561,12 @@ class TestIsEnum:
def test_enum(self):
"""Test is_enum with an enum."""
e = enum.Enum('Foo', 'bar, baz')
assert utils.is_enum(e)
class Foo(enum.Enum):
bar = 1
baz = 2
assert utils.is_enum(Foo)
def test_class(self):
"""Test is_enum with a non-enum class."""