Skip to content

Add type annotations #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
2dc1f0c
Add a few annotations.
Sachaa-Thanasius Jun 14, 2024
ae63851
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 14, 2024
c28dc0a
Add annotations to api.py.
Sachaa-Thanasius Jun 14, 2024
602934c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 14, 2024
be28283
Preliminary, but added minimalistic typing check to tox and CI, as we…
Sachaa-Thanasius Jun 14, 2024
719da58
Merge branch 'feature/just-annotations' of github.com:Sachaa-Thanasiu…
Sachaa-Thanasius Jun 14, 2024
9df0355
Add parameter annotations to builder.py.
Sachaa-Thanasius Jun 15, 2024
6c3d109
Add type annotations to `compat.to_str()` and `compat.to_bytes()`.
Sachaa-Thanasius Jun 15, 2024
a7622de
Finish annotating `query_items` parameters in builder.py.
Sachaa-Thanasius Jun 15, 2024
8cff806
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 15, 2024
57f5621
Account for current flake8 errors with noqa.
Sachaa-Thanasius Jun 15, 2024
f305405
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 15, 2024
c7ab1db
Annotate `misc.get_path()` while breaking an import cycle.
Sachaa-Thanasius Jun 15, 2024
c3b49f2
Add a comment explaining the reason `_QueryType` in builder.py is mod…
Sachaa-Thanasius Jun 15, 2024
dd96a34
Fix compat.py and api.py parameter annotations to accept bytearray as…
Sachaa-Thanasius Jun 15, 2024
d99f274
Started annotating IRIReference and URIReference.
Sachaa-Thanasius Jun 15, 2024
f4d150f
Fix: Removing the bytearray hints. Sticking to bytes and str is simpl…
Sachaa-Thanasius Jun 15, 2024
0179352
Partially annotated parseresult.py and _mixin.py.
Sachaa-Thanasius Jun 16, 2024
3969c1d
Finish annotating return types in builder.py.
Sachaa-Thanasius Jun 16, 2024
9e812f3
Made minor adjustments to a few annotations.
Sachaa-Thanasius Jun 16, 2024
9862d65
Fix port not being marked as `int` in several places.
Sachaa-Thanasius Jun 17, 2024
3c22353
More annotations that I forgot to break up into multiple commits.
Sachaa-Thanasius Jun 17, 2024
ee23708
Fix variable annotation for `uri` in `URIMixin`.copy_with.
Sachaa-Thanasius Jun 20, 2024
fadc962
Fix annotation for `misc.UseExisting` to be `Final` to avoid reassign…
Sachaa-Thanasius Jun 20, 2024
c923049
Change how port is determined/validated in `validators.subauthority_c…
Sachaa-Thanasius Jun 22, 2024
7fc9af0
Replace reorder-python-imports and flake8-import-order with isort, al…
Sachaa-Thanasius Jul 3, 2024
bf1a44d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 3, 2024
283f910
Add exclude lines to .coveragerc to account for a) `if t.TYPE_CHECKIN…
Sachaa-Thanasius Jul 3, 2024
6a4daf3
Merge branch 'feature/just-annotations' of github.com:Sachaa-Thanasiu…
Sachaa-Thanasius Jul 3, 2024
1443cd1
Add `#pragma: no cover` to final line missing coverage, as well as a …
Sachaa-Thanasius Jul 3, 2024
be1a4e8
Adjust typing check to use wrapper/shim script for now.
Sachaa-Thanasius Jul 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[tool.pyright]
include = ["src/rfc3986"]
ignore = ["tests"]
pythonVersion = "3.8"
typeCheckingMode = "strict"

reportPrivateUsage = "none"
reportImportCycles = "warning"
reportPropertyTypeMismatch = "warning"
reportUnnecessaryTypeIgnoreComment = "warning"
10 changes: 5 additions & 5 deletions src/rfc3986/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from .uri import URIReference


def uri_reference(uri, encoding="utf-8"):
def uri_reference(uri: str, encoding: str = "utf-8") -> URIReference:
"""Parse a URI string into a URIReference.

This is a convenience function. You could achieve the same end by using
Expand All @@ -36,7 +36,7 @@ def uri_reference(uri, encoding="utf-8"):
return URIReference.from_string(uri, encoding)


def iri_reference(iri, encoding="utf-8"):
def iri_reference(iri: str, encoding: str = "utf-8") -> IRIReference:
"""Parse a IRI string into an IRIReference.

This is a convenience function. You could achieve the same end by using
Expand All @@ -50,7 +50,7 @@ def iri_reference(iri, encoding="utf-8"):
return IRIReference.from_string(iri, encoding)


def is_valid_uri(uri, encoding="utf-8", **kwargs):
def is_valid_uri(uri: str, encoding: str = "utf-8", **kwargs: bool) -> bool:
"""Determine if the URI given is valid.

This is a convenience function. You could use either
Expand All @@ -75,7 +75,7 @@ def is_valid_uri(uri, encoding="utf-8", **kwargs):
return URIReference.from_string(uri, encoding).is_valid(**kwargs)


def normalize_uri(uri, encoding="utf-8"):
def normalize_uri(uri: str, encoding: str = "utf-8") -> str:
"""Normalize the given URI.

This is a convenience function. You could use either
Expand All @@ -91,7 +91,7 @@ def normalize_uri(uri, encoding="utf-8"):
return normalized_reference.unsplit()


def urlparse(uri, encoding="utf-8"):
def urlparse(uri: str, encoding: str = "utf-8") -> ParseResult:
"""Parse a given URI and return a ParseResult.

This is a partial replacement of the standard library's urlparse function.
Expand Down
39 changes: 27 additions & 12 deletions src/rfc3986/normalizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,21 @@
# limitations under the License.
"""Module with functions to normalize components."""
import re
import typing as t
from urllib.parse import quote as urlquote

from . import compat
from . import misc


def normalize_scheme(scheme):
def normalize_scheme(scheme: str) -> str:
"""Normalize the scheme component."""
return scheme.lower()


def normalize_authority(authority):
def normalize_authority(
authority: t.Tuple[t.Optional[str], t.Optional[str], t.Optional[str]],
) -> str:
"""Normalize an authority tuple to a string."""
userinfo, host, port = authority
result = ""
Expand All @@ -37,17 +40,17 @@ def normalize_authority(authority):
return result


def normalize_username(username):
def normalize_username(username: str) -> str:
"""Normalize a username to make it safe to include in userinfo."""
return urlquote(username)


def normalize_password(password):
def normalize_password(password: str) -> str:
"""Normalize a password to make safe for userinfo."""
return urlquote(password)


def normalize_host(host):
def normalize_host(host: str) -> str:
"""Normalize a host string."""
if misc.IPv6_MATCHER.match(host):
percent = host.find("%")
Expand All @@ -70,7 +73,7 @@ def normalize_host(host):
return host.lower()


def normalize_path(path):
def normalize_path(path: str) -> str:
"""Normalize the path string."""
if not path:
return path
Expand All @@ -79,14 +82,14 @@ def normalize_path(path):
return remove_dot_segments(path)


def normalize_query(query):
def normalize_query(query: str) -> str:
"""Normalize the query string."""
if not query:
return query
return normalize_percent_characters(query)


def normalize_fragment(fragment):
def normalize_fragment(fragment: str) -> str:
"""Normalize the fragment string."""
if not fragment:
return fragment
Expand All @@ -96,7 +99,7 @@ def normalize_fragment(fragment):
PERCENT_MATCHER = re.compile("%[A-Fa-f0-9]{2}")


def normalize_percent_characters(s):
def normalize_percent_characters(s: str) -> str:
"""All percent characters should be upper-cased.

For example, ``"%3afoo%DF%ab"`` should be turned into ``"%3Afoo%DF%AB"``.
Expand All @@ -108,14 +111,14 @@ def normalize_percent_characters(s):
return s


def remove_dot_segments(s):
def remove_dot_segments(s: str) -> str:
"""Remove dot segments from the string.

See also Section 5.2.4 of :rfc:`3986`.
"""
# See http://tools.ietf.org/html/rfc3986#section-5.2.4 for pseudo-code
segments = s.split("/") # Turn the path into a list of segments
output = [] # Initialize the variable to use to store output
output: list[str] = [] # Initialize the variable to use to store output

for segment in segments:
# '.' is the current directory, so ignore it, it is superfluous
Expand All @@ -142,7 +145,19 @@ def remove_dot_segments(s):
return "/".join(output)


def encode_component(uri_component, encoding):
@t.overload
def encode_component(uri_component: None, encoding: str) -> None:
...


@t.overload
def encode_component(uri_component: str, encoding: str) -> str:
...


def encode_component(
uri_component: t.Optional[str], encoding: str
) -> t.Optional[str]:
"""Encode the specific component in the provided encoding."""
if uri_component is None:
return uri_component
Expand Down
Loading