Skip to content

Commit

Permalink
Use typeguard during tests and in osc-wrapper.py
Browse files Browse the repository at this point in the history
  • Loading branch information
dmach committed Jan 9, 2024
1 parent 2408417 commit 0d6fdff
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ jobs:
- name: "Install packages"
run: |
sudo apt-get -y update
sudo apt-get -y --no-install-recommends install python3-behave diffstat diffutils python3 python3-cryptography python3-pip python3-rpm python3-setuptools python3-urllib3
sudo apt-get -y --no-install-recommends install python3-behave diffstat diffutils python3 python3-cryptography python3-pip python3-rpm python3-setuptools python3-typeguard python3-urllib3
- name: "Checkout sources"
uses: actions/checkout@v3
Expand Down
10 changes: 10 additions & 0 deletions contrib/osc.spec
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
%bcond_with fdupes
%endif

# use typeguard during build on distros where typeguard is available
%if (0%{?suse_version} > 1500 || 0%{?fedora} >= 37)
%bcond_without typeguard
%else
%bcond_with typeguard
%endif

%define argparse_manpage_pkg argparse-manpage
%define obs_build_pkg obs-build
%define openssh_pkg openssh
Expand Down Expand Up @@ -68,6 +75,9 @@ BuildRequires: %{use_python_pkg}-cryptography
BuildRequires: %{use_python_pkg}-devel >= 3.6
BuildRequires: %{use_python_pkg}-rpm
BuildRequires: %{use_python_pkg}-setuptools
%if %{with typeguard}
BuildRequires: %{use_python_pkg}-typeguard
%endif
BuildRequires: %{use_python_pkg}-urllib3
BuildRequires: diffstat
%if %{with fdupes}
Expand Down
22 changes: 22 additions & 0 deletions osc-wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,28 @@
This wrapper allows osc to be called from the source directory during development.
"""


# developers and early adopters have typeguard enabled
# so they can catch and report issues early
try:
from typeguard import install_import_hook
except ImportError:
install_import_hook = None

if install_import_hook is None:
try:
from typeguard.importhook import install_import_hook
except ImportError:
install_import_hook = None

if install_import_hook:
# install typeguard import hook only if available
install_import_hook("osc")
else:
import osc.oscerr
raise osc.oscerr.PackageNotInstalled("python3-typeguard")


import osc.babysitter

osc.babysitter.main()
28 changes: 14 additions & 14 deletions osc/commandline.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,10 +584,10 @@ def pop_args(
args,
arg1_name: str = None,
arg1_is_optional: bool = False,
arg1_default: str = None,
arg1_default: Optional[str] = None,
arg2_name: str = None,
arg2_is_optional: bool = False,
arg2_default: str = None,
arg2_default: Optional[str] = None,
):
"""
Pop 2 arguments from `args`.
Expand Down Expand Up @@ -658,9 +658,9 @@ def pop_args(
def pop_project_package_from_args(
args: List[str],
project_is_optional: bool = False,
default_project: str = None,
default_project: Optional[str] = None,
package_is_optional: bool = False,
default_package: str = None,
default_package: Optional[str] = None,
):
"""
Pop project and package from given `args`.
Expand Down Expand Up @@ -729,9 +729,9 @@ def pop_project_package_from_args(
def pop_repository_arch_from_args(
args: List[str],
repository_is_optional: bool = False,
default_repository: str = None,
default_repository: Optional[str] = None,
arch_is_optional: bool = False,
default_arch: str = None,
default_arch: Optional[str] = None,
):
"""
Pop repository and arch from given `args`.
Expand Down Expand Up @@ -768,13 +768,13 @@ def pop_repository_arch_from_args(
def pop_project_package_repository_arch_from_args(
args: List[str],
project_is_optional: bool = False,
default_project: str = None,
default_project: Optional[str] = None,
package_is_optional: bool = False,
default_package: str = None,
default_package: Optional[str] = None,
repository_is_optional: bool = False,
default_repository: str = None,
default_repository: Optional[str] = None,
arch_is_optional: bool = False,
default_arch: str = None,
default_arch: Optional[str] = None,
):
"""
Pop project, package, repository and arch from given `args`.
Expand Down Expand Up @@ -854,13 +854,13 @@ def pop_project_package_repository_arch_from_args(
def pop_project_package_targetproject_targetpackage_from_args(
args: List[str],
project_is_optional: bool = False,
default_project: str = None,
default_project: Optional[str] = None,
package_is_optional: bool = False,
default_package: str = None,
default_package: Optional[str] = None,
target_project_is_optional: bool = False,
default_target_project: str = None,
default_target_project: Optional[str] = None,
target_package_is_optional: bool = False,
default_target_package: str = None,
default_target_package: Optional[str] = None,
):
"""
Pop project, package, target project and target package from given `args`.
Expand Down
28 changes: 14 additions & 14 deletions osc/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,7 @@ def __str__(self):
@staticmethod
def init_project(
apiurl: str,
dir: Path,
dir: str,
project,
package_tracking=True,
getPackageList=True,
Expand Down Expand Up @@ -2176,7 +2176,7 @@ def status(self, n):

return state

def get_diff(self, revision=None, ignoreUnversioned=False):
def get_diff(self, revision: Optional[int] = None, ignoreUnversioned=False):
diff_hdr = b'Index: %s\n'
diff_hdr += b'===================================================================\n'
kept = []
Expand Down Expand Up @@ -2274,7 +2274,7 @@ def diff_add_delete(fname, add, revision):
continue
elif state == ' ' and revision is None:
continue
elif not revision_is_empty(revision) and self.findfilebyname(f.name).md5 == f.md5 and state != 'M':
elif not revision_is_empty(revision) and self.findfilebyname(f.name).md5 == f.md5 and state != "M":
continue
yield [diff_hdr % f.name.encode()]
if revision is None:
Expand Down Expand Up @@ -3739,7 +3739,7 @@ def meta_get_project_list(apiurl: str, deleted=False):
return sorted(node.get('name') for node in root if node.get('name'))


def show_project_meta(apiurl: str, prj: str, rev=None, blame=None):
def show_project_meta(apiurl: str, prj: str, rev: Optional[int] = None, blame=None):
query = {}
if blame:
query['view'] = "blame"
Expand All @@ -3766,7 +3766,7 @@ def show_project_meta(apiurl: str, prj: str, rev=None, blame=None):
return f.readlines()


def show_project_conf(apiurl: str, prj: str, rev=None, blame=None):
def show_project_conf(apiurl: str, prj: str, rev: Optional[int] = None, blame=None):
query = {}
url = None
if not revision_is_empty(rev):
Expand Down Expand Up @@ -4196,7 +4196,7 @@ def show_files_meta(
apiurl: str,
prj: str,
pac: str,
revision=None,
revision: Optional[int] = None,
expand=False,
linkrev=None,
linkrepair=False,
Expand Down Expand Up @@ -4843,7 +4843,7 @@ def get_request_collection(
package=None,
states=None,
review_states=None,
types: List[str] = None,
types: Optional[List[str]] = None,
ids=None,
withfullhistory=False
):
Expand Down Expand Up @@ -5388,18 +5388,18 @@ def server_diff(
apiurl: str,
old_project: str,
old_package: str,
old_revision: str,
old_revision: Optional[str],
new_project: str,
new_package: str,
new_revision: str,
new_revision: Optional[str],
unified=False,
missingok=False,
meta=False,
expand=True,
onlyissues=False,
full=True,
xml=False,
files: list = None,
files: Optional[list] = None,
):
query: Dict[str, Union[str, int]] = {"cmd": "diff"}
if expand:
Expand Down Expand Up @@ -5454,17 +5454,17 @@ def server_diff_noex(
apiurl: str,
old_project: str,
old_package: str,
old_revision: str,
old_revision: Optional[str],
new_project: str,
new_package: str,
new_revision: str,
new_revision: Optional[str],
unified=False,
missingok=False,
meta=False,
expand=True,
onlyissues=False,
xml=False,
files: list = None,
files: Optional[list] = None,
):
try:
return server_diff(apiurl,
Expand Down Expand Up @@ -7709,7 +7709,7 @@ def owner(
return res


def set_link_rev(apiurl: str, project: str, package: str, revision="", expand=False, msg: str=None):
def set_link_rev(apiurl: str, project: str, package: str, revision="", expand=False, msg: Optional[str] = None):
url = makeurl(apiurl, ["source", project, package, "_link"])
try:
f = http_GET(url)
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ install_requires =
cryptography
# rpm is not available on pip, install a matching package manually prior installing osc
rpm
typeguard
urllib3

[options.extras_require]
Expand Down
14 changes: 14 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
try:
from typeguard import install_import_hook
except ImportError:
install_import_hook = None

if not install_import_hook:
try:
from typeguard.importhook import install_import_hook
except ImportError:
install_import_hook = None

if install_import_hook:
# install typeguard import hook only if available
install_import_hook("osc")

0 comments on commit 0d6fdff

Please sign in to comment.