From 895bca36541598a04dba525568a20d2282e0ffd9 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 31 Mar 2021 05:18:07 +0200 Subject: [PATCH] [mypy] Fix web_programming directory (#4297) * Update world_covid19_stats.py * Delete monkeytype_config.py * updating DIRECTORY.md * Apply pyannotate suggestions to emails_from_url.py * mypy web_programming/emails_from_url.py * super().__init__() * mypy --ignore-missing-imports web_programming/emails_from_url.py * Update emails_from_url.py * self.urls: list[str] = [] * mypy: Fix web_programming directory Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Dhruv Manilawala --- .github/workflows/build.yml | 2 +- web_programming/currency_converter.py | 2 +- web_programming/emails_from_url.py | 19 ++++++++++--------- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f544c02b1..76c6357fe 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -23,7 +23,7 @@ jobs: python -m pip install mypy pytest-cov -r requirements.txt # FIXME: #4052 fix mypy errors in the exclude directories and remove them below - run: mypy --ignore-missing-imports - --exclude '(arithmetic_analysis|ciphers|conversions|data_structures|digital_image_processing|dynamic_programming|graphs|hashes|linear_algebra|maths|matrix|other|project_euler|scripts|searches|strings|web_programming*)/$' . + --exclude '(arithmetic_analysis|ciphers|conversions|data_structures|digital_image_processing|dynamic_programming|graphs|hashes|linear_algebra|maths|matrix|other|project_euler|scripts|searches|strings*)/$' . - name: Run tests run: pytest --doctest-modules --ignore=project_euler/ --ignore=scripts/ --cov-report=term-missing:skip-covered --cov=. . - if: ${{ success() }} diff --git a/web_programming/currency_converter.py b/web_programming/currency_converter.py index 6aed2a557..447595b0b 100644 --- a/web_programming/currency_converter.py +++ b/web_programming/currency_converter.py @@ -9,7 +9,7 @@ import requests URL_BASE = "https://www.amdoren.com/api/currency.php" TESTING = os.getenv("CI", False) -API_KEY = os.getenv("AMDOREN_API_KEY") +API_KEY = os.getenv("AMDOREN_API_KEY", "") if not API_KEY and not TESTING: raise KeyError("Please put your API key in an environment variable.") diff --git a/web_programming/emails_from_url.py b/web_programming/emails_from_url.py index 01dee274f..0571ac331 100644 --- a/web_programming/emails_from_url.py +++ b/web_programming/emails_from_url.py @@ -8,18 +8,19 @@ __status__ = "Alpha" import re from html.parser import HTMLParser +from typing import Optional from urllib import parse import requests class Parser(HTMLParser): - def __init__(self, domain: str): - HTMLParser.__init__(self) - self.data = [] + def __init__(self, domain: str) -> None: + super().__init__() + self.urls: list[str] = [] self.domain = domain - def handle_starttag(self, tag: str, attrs: str) -> None: + def handle_starttag(self, tag: str, attrs: list[tuple[str, Optional[str]]]) -> None: """ This function parse html to take takes url from tags """ @@ -29,10 +30,10 @@ class Parser(HTMLParser): for name, value in attrs: # If href is defined, and not empty nor # print it. if name == "href" and value != "#" and value != "": - # If not already in data. - if value not in self.data: + # If not already in urls. + if value not in self.urls: url = parse.urljoin(self.domain, value) - self.data.append(url) + self.urls.append(url) # Get main domain name (example.com) @@ -59,7 +60,7 @@ def get_sub_domain_name(url: str) -> str: return parse.urlparse(url).netloc -def emails_from_url(url: str = "https://github.com") -> list: +def emails_from_url(url: str = "https://github.com") -> list[str]: """ This function takes url and return all valid urls """ @@ -78,7 +79,7 @@ def emails_from_url(url: str = "https://github.com") -> list: # Get links and loop through valid_emails = set() - for link in parser.data: + for link in parser.urls: # open URL. # read = requests.get(link) try: