Merge pull request #4398 from mhils/pdoc

duplicate hooks: warn instead of raise
This commit is contained in:
Maximilian Hils 2021-01-16 20:37:52 +01:00 committed by GitHub
commit a92279e69f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 7 additions and 4 deletions

View File

@ -1,4 +1,5 @@
import re import re
import warnings
from dataclasses import dataclass, is_dataclass, fields from dataclasses import dataclass, is_dataclass, fields
from typing import ClassVar, Any, Dict, Type, Set, List, TYPE_CHECKING, Sequence from typing import ClassVar, Any, Dict, Type, Set, List, TYPE_CHECKING, Sequence
@ -32,7 +33,7 @@ class Hook:
cls.name = re.sub('(?!^)([A-Z]+)', r'_\1', name).lower() cls.name = re.sub('(?!^)([A-Z]+)', r'_\1', name).lower()
if cls.name in all_hooks: if cls.name in all_hooks:
other = all_hooks[cls.name] other = all_hooks[cls.name]
raise RuntimeError(f"Two conflicting event classes for {cls.name}: {cls} and {other}") warnings.warn(f"Two conflicting event classes for {cls.name}: {cls} and {other}", RuntimeWarning)
if cls.name == "": if cls.name == "":
return # don't register Hook class. return # don't register Hook class.
all_hooks[cls.name] = cls all_hooks[cls.name] = cls

View File

@ -4,6 +4,7 @@ from abc import ABCMeta
from enum import Flag from enum import Flag
from typing import List, Literal, Optional, Sequence, Tuple, Union, TYPE_CHECKING from typing import List, Literal, Optional, Sequence, Tuple, Union, TYPE_CHECKING
import mitmproxy
from mitmproxy import certs from mitmproxy import certs
from mitmproxy.coretypes import serializable from mitmproxy.coretypes import serializable
from mitmproxy.net import server_spec from mitmproxy.net import server_spec

View File

@ -5,6 +5,7 @@ The counterpart to events are commands.
""" """
import socket import socket
import typing import typing
import warnings
from dataclasses import dataclass, is_dataclass from dataclasses import dataclass, is_dataclass
from mitmproxy.proxy import commands from mitmproxy.proxy import commands
@ -78,7 +79,7 @@ class CommandCompleted(Event):
raise RuntimeError(f"{command_cls} needs a properly annotated command attribute.") raise RuntimeError(f"{command_cls} needs a properly annotated command attribute.")
if command_cls in command_reply_subclasses: if command_cls in command_reply_subclasses:
other = command_reply_subclasses[command_cls] other = command_reply_subclasses[command_cls]
raise RuntimeError(f"Two conflicting subclasses for {command_cls}: {cls} and {other}") warnings.warn(f"Two conflicting subclasses for {command_cls}: {cls} and {other}", RuntimeWarning)
command_reply_subclasses[command_cls] = cls command_reply_subclasses[command_cls] = cls
def __repr__(self): def __repr__(self):

View File

@ -31,6 +31,6 @@ def test_command_completed():
class FooCompleted1(events.CommandCompleted): class FooCompleted1(events.CommandCompleted):
command: FooCommand command: FooCommand
with pytest.raises(RuntimeError, match="conflicting subclasses"): with pytest.warns(RuntimeWarning, match="conflicting subclasses"):
class FooCompleted2(events.CommandCompleted): class FooCompleted2(events.CommandCompleted):
command: FooCommand command: FooCommand

View File

@ -24,7 +24,7 @@ def test_hook():
assert e.args() == [b"foo"] assert e.args() == [b"foo"]
assert FooHook in hooks.all_hooks.values() assert FooHook in hooks.all_hooks.values()
with pytest.raises(RuntimeError, match="Two conflicting event classes"): with pytest.warns(RuntimeWarning, match="Two conflicting event classes"):
@dataclass @dataclass
class FooHook2(hooks.Hook): class FooHook2(hooks.Hook):
name = "foo" name = "foo"