Skip to content

Commit

Permalink
Add: Introduce a pontos module for additional typings
Browse files Browse the repository at this point in the history
Currently the module contains the SupportsStr protocol. Personally I
don't know why this protocol isn't available in the standard lib.
  • Loading branch information
bjoernricks committed Jul 4, 2023
1 parent 35eb914 commit b03b841
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 9 deletions.
11 changes: 2 additions & 9 deletions pontos/changelog/conventional_commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@


import re
from abc import abstractmethod
from datetime import date
from pathlib import Path
from typing import Dict, List, Optional, Protocol, Union, runtime_checkable
from typing import Dict, List, Optional, Union

import tomlkit

from pontos.changelog.errors import ChangelogBuilderError
from pontos.git import Git
from pontos.typing import SupportsStr

ADDRESS = "https://github.com/"

Expand All @@ -39,13 +39,6 @@
"""


@runtime_checkable
class SupportsStr(Protocol):
@abstractmethod
def __str__(self) -> str:
pass


class ChangelogBuilder:
"""
Creates Changelog from conventional commits using the git log
Expand Down
30 changes: 30 additions & 0 deletions pontos/typing/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright (C) 2023 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from abc import abstractmethod
from typing import Protocol, runtime_checkable


@runtime_checkable
class SupportsStr(Protocol):
"""
A protocol for classes supporting __str__
"""

@abstractmethod
def __str__(self) -> str:
pass
16 changes: 16 additions & 0 deletions tests/typing/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (C) 2023 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
35 changes: 35 additions & 0 deletions tests/typing/test_typing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright (C) 2023 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import unittest

from pontos.typing import SupportsStr


class SupportsStrTestCase(unittest.TestCase):
def test_str(self):
self.assertIsInstance("", SupportsStr)
self.assertIsInstance(None, SupportsStr)

def test_some_class(self):
class Foo:
def __str__(self) -> str:
pass

foo = Foo()

self.assertIsInstance(foo, SupportsStr)

0 comments on commit b03b841

Please sign in to comment.