Skip to content

Commit

Permalink
Make BaseFileProxySource subclasses accept only one positional argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Some User committed Dec 20, 2022
1 parent 42e9ac2 commit f5d1c53
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
36 changes: 29 additions & 7 deletions proxylist/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from abc import abstractmethod
from collections.abc import Sequence
from http.client import HTTPException
from typing import IO, Any, cast
from typing import IO, cast
from urllib.error import URLError
from urllib.request import urlopen

Expand All @@ -20,6 +20,7 @@
class BaseFileProxySource(BaseProxySource):
def __init__(
self,
*,
proxy_type: None | str = None,
proxy_auth: None | tuple[str, str] = None,
) -> None:
Expand All @@ -43,9 +44,16 @@ def get_servers_list(self) -> list[ProxyServer]:
class LocalFileProxySource(BaseFileProxySource):
"""Load list from the file."""

def __init__(self, path: str, **kwargs: Any) -> None:
def __init__(
self,
path: str,
/,
*,
proxy_type: None | str = None,
proxy_auth: None | tuple[str, str] = None,
) -> None:
self.path = path
super().__init__(**kwargs)
super().__init__(proxy_type=proxy_type, proxy_auth=proxy_auth)

def load_content(self) -> str:
with open(self.path, encoding="utf-8") as inp:
Expand All @@ -55,9 +63,16 @@ def load_content(self) -> str:
class NetworkFileProxySource(BaseFileProxySource):
"""Load list from web resource."""

def __init__(self, url: str, **kwargs: Any) -> None:
def __init__(
self,
url: str,
/,
*,
proxy_type: None | str = None,
proxy_auth: None | tuple[str, str] = None,
) -> None:
self.url = url
super().__init__(**kwargs)
super().__init__(proxy_type=proxy_type, proxy_auth=proxy_auth)

def load_content(self) -> str:
recent_err = None
Expand All @@ -78,9 +93,16 @@ def load_content(self) -> str:
class LinesListProxySource(BaseFileProxySource):
"""Load list from python list of strings."""

def __init__(self, lines: Sequence[str], **kwargs: Any) -> None:
def __init__(
self,
lines: Sequence[str],
/,
*,
proxy_type: None | str = None,
proxy_auth: None | tuple[str, str] = None,
) -> None:
self._lines = lines
super().__init__(**kwargs)
super().__init__(proxy_type=proxy_type, proxy_auth=proxy_auth)

def load_content(self) -> str:
return "\n".join(self._lines)
22 changes: 21 additions & 1 deletion tests/test_source.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
from __future__ import annotations

from typing import Any

import pytest
from test_server import Response, TestServer

from proxylist.errors import ProxySourceReadError
from proxylist.source import NetworkFileProxySource
from proxylist.source import (
LinesListProxySource,
LocalFileProxySource,
NetworkFileProxySource,
)


def test_url_error() -> None:
Expand All @@ -18,3 +24,17 @@ def test_url_error() -> None:
NetworkFileProxySource(serv.get_url()).load_content()
finally:
serv.stop()


@pytest.mark.parametrize(
"source,inp",
[
(LinesListProxySource, "var/proxy.txt"),
(LocalFileProxySource, "https://example.com/proxy.txt"),
(NetworkFileProxySource, ["server:88"]),
],
)
def test_positional_parameters(source: type[Any], inp: str | list[str]) -> None:
with pytest.raises(TypeError) as ex:
source(inp, "socks5")
assert "takes 2 positional" in str(ex.value)

0 comments on commit f5d1c53

Please sign in to comment.