From 25af9ff6e70fed2a0e8637c73c0c17d9df5a00f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= <6774676+eumiro@users.noreply.github.com> Date: Mon, 8 Feb 2021 22:12:52 +0100 Subject: [PATCH 1/5] Make methods static --- overpy/__init__.py | 3 ++- tests/__init__.py | 2 +- tests/base_class.py | 27 ++++++++++++++++++--------- tests/test_xml.py | 3 ++- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/overpy/__init__.py b/overpy/__init__.py index 4909f67..9e83a4e 100644 --- a/overpy/__init__.py +++ b/overpy/__init__.py @@ -86,7 +86,8 @@ def __init__(self, read_chunk_size=None, url=None, xml_parser=XML_PARSER_SAX, ma self.xml_parser = xml_parser - def _handle_remark_msg(self, msg): + @staticmethod + def _handle_remark_msg(msg): """ Try to parse the message provided with the remark tag or element. diff --git a/tests/__init__.py b/tests/__init__.py index 8cb357d..287b0f3 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -21,7 +21,7 @@ def handle(self): self.request.send(data) @staticmethod - def get_response(self): + def get_response(): yield b"" diff --git a/tests/base_class.py b/tests/base_class.py index 03f6b59..be89a1a 100644 --- a/tests/base_class.py +++ b/tests/base_class.py @@ -10,7 +10,8 @@ class BaseTestAreas: - def _test_area01(self, result): + @staticmethod + def _test_area01(result): assert len(result.areas) == 4 assert len(result.nodes) == 0 assert len(result.relations) == 0 @@ -77,7 +78,8 @@ def _test_area01(self, result): class BaseTestNodes: - def _test_node01(self, result): + @staticmethod + def _test_node01(result): assert len(result.nodes) == 3 assert len(result.relations) == 0 assert len(result.ways) == 0 @@ -151,7 +153,8 @@ def _test_node01(self, result): class BaseTestRelation: - def _test_relation01(self, result): + @staticmethod + def _test_relation01(result): assert len(result.nodes) == 0 assert len(result.relations) == 1 assert len(result.ways) == 0 @@ -187,7 +190,8 @@ def _test_relation01(self, result): assert isinstance(relation.members[3], overpy.RelationNode) assert isinstance(relation.members[4], overpy.RelationWay) - def _test_relation02(self, result): + @staticmethod + def _test_relation02(result): assert len(result.nodes) == 3 assert len(result.relations) == 1 assert len(result.ways) == 1 @@ -240,7 +244,8 @@ def _test_relation02(self, result): assert way.id == 317146078 assert member.ref == way.id - def _test_relation03(self, result): + @staticmethod + def _test_relation03(result): assert len(result.nodes) == 0 assert len(result.relations) == 1 assert len(result.ways) == 0 @@ -259,7 +264,8 @@ def _test_relation03(self, result): assert relation.center_lat == Decimal("50.8176646") assert relation.center_lon == Decimal("7.0208539") - def _test_relation04(self, result): + @staticmethod + def _test_relation04(result): assert len(result.nodes) == 0 assert len(result.relations) == 1 assert len(result.ways) == 0 @@ -289,7 +295,8 @@ def _test_relation04(self, result): class BaseTestWay: - def _test_way01(self, result): + @staticmethod + def _test_way01(result): assert len(result.nodes) == 0 assert len(result.relations) == 0 assert len(result.ways) == 2 @@ -345,7 +352,8 @@ def _test_way01(self, result): assert way_ids[0] == 317146077 assert way_ids[1] == 317146078 - def _test_way02(self, result): + @staticmethod + def _test_way02(result): assert len(result.nodes) == 6 assert len(result.relations) == 0 assert len(result.ways) == 1 @@ -405,7 +413,8 @@ def _test_way02(self, result): assert len(way_ids) == 1 assert way_ids[0] == 317146077 - def _test_way03(self, result): + @staticmethod + def _test_way03(result): assert len(result.nodes) == 4 assert len(result.relations) == 0 assert len(result.ways) == 1 diff --git a/tests/test_xml.py b/tests/test_xml.py index c199e68..7c3c9db 100644 --- a/tests/test_xml.py +++ b/tests/test_xml.py @@ -106,7 +106,8 @@ def test_way04(self): class TestDataError: - def _get_element_wrong_type(self): + @staticmethod + def _get_element_wrong_type(): data = "" import xml.etree.ElementTree as ET return ET.fromstring(data) From f7e5cbba2cd8884d85f421c1d148e28243c9d0d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= <6774676+eumiro@users.noreply.github.com> Date: Mon, 8 Feb 2021 22:13:08 +0100 Subject: [PATCH 2/5] Remove redundant parentheses --- overpy/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/overpy/__init__.py b/overpy/__init__.py index 9e83a4e..8c1e7ba 100644 --- a/overpy/__init__.py +++ b/overpy/__init__.py @@ -629,7 +629,7 @@ def get_center_from_json(cls, data): raise ValueError("Unable to get lat or lon of way center.") center_lat = Decimal(center_lat) center_lon = Decimal(center_lon) - return (center_lat, center_lon) + return center_lat, center_lon @classmethod def get_center_from_xml_dom(cls, sub_child): From 1aeeb4b95660550fb9353e4e53c27bec0ed1fa6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= <6774676+eumiro@users.noreply.github.com> Date: Mon, 8 Feb 2021 22:13:20 +0100 Subject: [PATCH 3/5] Fix None comparison --- tests/test_exception.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_exception.py b/tests/test_exception.py index afe5682..158588d 100644 --- a/tests/test_exception.py +++ b/tests/test_exception.py @@ -5,7 +5,7 @@ class TestExceptions: def test_element_data_wrong_type(self): e = overpy.exception.ElementDataWrongType("from1") assert e.type_expected == "from1" - assert e.type_provided == None + assert e.type_provided is None assert isinstance(str(e), str) e = overpy.exception.ElementDataWrongType("from2", "to2") @@ -28,7 +28,7 @@ def test_overpass_bad_request(self): def test_overpass_unknown_content_type(self): e = overpy.exception.OverpassUnknownContentType(None) - assert e.content_type == None + assert e.content_type is None assert str(e).startswith("No content") e = overpy.exception.OverpassUnknownContentType("content") From 1ea063b4d381299a97c2ef7015428acea2bf4b38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= <6774676+eumiro@users.noreply.github.com> Date: Mon, 8 Feb 2021 22:42:57 +0100 Subject: [PATCH 4/5] Use pathlib.Path --- docs/source/conf.py | 12 ++---------- setup.py | 14 ++++---------- tests/__init__.py | 5 ++--- 3 files changed, 8 insertions(+), 23 deletions(-) diff --git a/docs/source/conf.py b/docs/source/conf.py index 99dc446..9c1e458 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -5,21 +5,13 @@ # serve to show the default. import sys -import os +from pathlib import Path # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. #sys.path.insert(0, os.path.abspath('.')) -sys.path.insert( - 0, - os.path.abspath( - os.path.join( - os.path.dirname(__file__), - '../../' - ) - ) -) +sys.path.insert(0, str(Path(__file__).resolve().parent.parent.parent)) # -- General configuration ------------------------------------------------ diff --git a/setup.py b/setup.py index d1b0014..b74ee3c 100644 --- a/setup.py +++ b/setup.py @@ -1,21 +1,15 @@ #!/usr/bin/env python import os import sys +from pathlib import Path from setuptools import setup, find_packages -base_dir = os.path.dirname(__file__) +HERE = Path(__file__).resolve().parent about = {} -with open(os.path.join(base_dir, "overpy", "__about__.py")) as f: - exec(f.read(), about) +exec((HERE / "overpy" / "__about__.py").read_text(), about) -filename_readme = os.path.join(base_dir, "README.rst") -if sys.version_info[0] == 2: - import io - fp = open(filename_readme, encoding="utf-8") -else: - fp = open(filename_readme, encoding="utf-8") -long_description = fp.read() +long_description = (HERE / "README.rst").read_text() setup( name=about["__title__"], diff --git a/tests/__init__.py b/tests/__init__.py index 287b0f3..d59ab53 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,6 +1,6 @@ -import os import sys import threading +from pathlib import Path from threading import Lock from socketserver import BaseRequestHandler, TCPServer, ThreadingMixIn @@ -26,8 +26,7 @@ def get_response(): def read_file(filename, mode="r"): - filename = os.path.join(os.path.dirname(__file__), filename) - return open(filename, mode).read() + return (Path(__file__).resolve().parent / filename).open(mode).read() def new_server_thread(handle_cls, port=None): From 1450a0ce88756a73dbaa5cb083a495ea0748065f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= <6774676+eumiro@users.noreply.github.com> Date: Mon, 8 Feb 2021 22:16:34 +0100 Subject: [PATCH 5/5] Fix import order --- docs/source/conf.py | 3 ++- overpy/__init__.py | 5 ++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/source/conf.py b/docs/source/conf.py index 9c1e458..382cc59 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -7,6 +7,8 @@ import sys from pathlib import Path +from overpy import __about__ as overpy_about + # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. @@ -49,7 +51,6 @@ # built documents. # # The short X.Y version. -from overpy import __about__ as overpy_about version = overpy_about.__version__ # The full version, including alpha/beta/rc tags. release = overpy_about.__version__ diff --git a/overpy/__init__.py b/overpy/__init__.py index 8c1e7ba..a041e04 100644 --- a/overpy/__init__.py +++ b/overpy/__init__.py @@ -1,6 +1,8 @@ from collections import OrderedDict from datetime import datetime from decimal import Decimal +from urllib.request import urlopen +from urllib.error import HTTPError from xml.sax import handler, make_parser import json import re @@ -25,9 +27,6 @@ "visible": lambda v: v.lower() == "true" } -from urllib.request import urlopen -from urllib.error import HTTPError - def is_valid_type(element, cls): """