Skip to content
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

WIP: add a few type hints #1383

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 10 additions & 5 deletions osc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import re
import sys
from io import StringIO
from typing import Optional, Tuple
from urllib.parse import urlsplit

from . import credentials
Expand Down Expand Up @@ -457,7 +458,11 @@ def apply_option_types(config, conffile=""):
"""


def parse_apisrv_url(scheme, apisrv):
def parse_apisrv_url(scheme: Optional[str], apisrv: str) -> Tuple[str, str, str]:
"""Parses the api server url ``apisrv`` and returns the tuple (scheme, url,
path).

"""
if apisrv.startswith('http://') or apisrv.startswith('https://'):
url = apisrv
elif scheme is not None:
Expand All @@ -468,17 +473,17 @@ def parse_apisrv_url(scheme, apisrv):
return scheme, url, path.rstrip('/')


def urljoin(scheme, apisrv, path=''):
return '://'.join([scheme, apisrv]) + path
def urljoin(scheme: str, apisrv: str, path: str='') -> str:
return f"{scheme}://{apisrv}{path}"


def is_known_apiurl(url):
def is_known_apiurl(url: str) -> bool:
"""returns ``True`` if url is a known apiurl"""
apiurl = urljoin(*parse_apisrv_url(None, url))
return apiurl in config['api_host_options']


def extract_known_apiurl(url):
def extract_known_apiurl(url: str) -> Optional[str]:
"""
Return longest prefix of given url that is known apiurl,
None if there is no known apiurl that is prefix of given url.
Expand Down
76 changes: 67 additions & 9 deletions osc/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
from http.client import IncompleteRead
from io import StringIO
from pathlib import Path
from typing import Optional, Dict, Union, List, Iterable
from typing import Optional, Dict, Union, List, Iterable, Tuple, overload
from typing_extensions import Literal, TypedDict
from urllib.parse import urlsplit, urlunsplit, urlparse, quote_plus, urlencode, unquote
from urllib.error import HTTPError
from urllib.request import pathname2url
Expand Down Expand Up @@ -3760,7 +3761,7 @@ def show_package_trigger_reason(apiurl: str, prj: str, pac: str, repo: str, arch
raise


def show_package_meta(apiurl: str, prj: str, pac: str, meta=False, blame=None):
def show_package_meta(apiurl: str, prj: str, pac: str, meta: bool=False, blame=None) -> List[bytes]:
query: Dict[str, Union[str, int]] = {}
if meta:
query['meta'] = 1
Expand Down Expand Up @@ -3822,7 +3823,7 @@ def show_scmsync(apiurl, prj, pac=None):
return node.text


def show_devel_project(apiurl, prj, pac):
def show_devel_project(apiurl: str, prj: str, pac: str) -> Tuple[Optional[str], Optional[str]]:
m = show_package_meta(apiurl, prj, pac)
node = ET.fromstring(b''.join(m)).find('devel')
if node is None:
Expand Down Expand Up @@ -3928,7 +3929,7 @@ def is_force_supported(self):
def __call__(self, **kwargs):
return self._delegate(**kwargs)

def __init__(self, url, input, change_is_required=False, file_ext='.xml', method=None):
def __init__(self, url, input: Union[List[str], str, bytes, List[bytes]], change_is_required=False, file_ext='.xml', method=None):
if isinstance(url, self._URLFactory):
self._url_factory = url
else:
Expand Down Expand Up @@ -4015,8 +4016,17 @@ def discard(self):
os.unlink(self.filename)


MetaType = Literal['prj', 'pkg', 'attribute', 'prjconf', 'user', 'group', 'pattern']

class _MetaType(TypedDict):
path: str
template: str
file_ext: str


# different types of metadata
metatypes = {'prj': {'path': 'source/%s/_meta',
metatypes: Dict[MetaType, _MetaType] = {
'prj': {'path': 'source/%s/_meta',
'template': new_project_templ,
'file_ext': '.xml'
},
Expand Down Expand Up @@ -4047,7 +4057,7 @@ def discard(self):
}


def meta_exists(metatype: str, path_args=None, template_args=None, create_new=True, apiurl=None):
def meta_exists(metatype: MetaType, path_args=None, template_args=None, create_new=True, apiurl=None):

global metatypes

Expand All @@ -4068,7 +4078,7 @@ def meta_exists(metatype: str, path_args=None, template_args=None, create_new=Tr


def make_meta_url(
metatype: str,
metatype: MetaType,
path_args=None,
apiurl: Optional[str] = None,
force=False,
Expand Down Expand Up @@ -4111,7 +4121,7 @@ def parse_meta_to_string(data: Union[bytes, list, Iterable]) -> str:


def edit_meta(
metatype,
metatype: MetaType,
path_args=None,
data: Optional[List[str]] = None,
template_args=None,
Expand Down Expand Up @@ -6050,6 +6060,7 @@ def attribute_branch_pkg(
return r


@overload
def branch_pkg(
apiurl: str,
src_project: str,
Expand All @@ -6071,7 +6082,54 @@ def branch_pkg(
maintenance=False,
newinstance=False,
disable_build=False,
):
) -> Tuple[False, Optional[str], Optional[str], Optional[str], Optional[str]]: ...

Check notice

Code scanning / CodeQL

Statement has no effect

This statement has no effect.

@overload
def branch_pkg(
apiurl: str,
src_project: str,
src_package: str,
nodevelproject=False,
rev=None,
linkrev=None,
target_project: Optional[str] = None,
target_package=None,
return_existing=True,
msg="",
force=False,
noaccess=False,
add_repositories=False,
add_repositories_block=None,
add_repositories_rebuild=None,
extend_package_names=False,
missingok=False,
maintenance=False,
newinstance=False,
disable_build=False,
) -> Union[Tuple[True, str, str, None, None], Tuple[False, Optional[str], Optional[str], Optional[str], Optional[str]]]: ...

Check notice

Code scanning / CodeQL

Statement has no effect

This statement has no effect.

def branch_pkg(
apiurl: str,
src_project: str,
src_package: str,
nodevelproject=False,
rev=None,
linkrev=None,
target_project: Optional[str] = None,
target_package=None,
return_existing=False,
msg="",
force=False,
noaccess=False,
add_repositories=False,
add_repositories_block=None,
add_repositories_rebuild=None,
extend_package_names=False,
missingok=False,
maintenance=False,
newinstance=False,
disable_build=False,
) -> Union[Tuple[True, str, str, None, None], Tuple[False, Optional[str], Optional[str], Optional[str], Optional[str]]]:
"""
Branch a package (via API call)
"""
Expand Down