-
Notifications
You must be signed in to change notification settings - Fork 5
🎈 Add Docstrings to Anrede Enum Members #75
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
79f3243
wip
hf-kklein 32f8e79
more assertions. helpless comment
hf-kklein ef019e1
more docs
hf-kklein e449b85
more explicit tests
hf-kklein 0297b30
Update tests/test_enums.py
hf-kklein b66530b
move around classes
hf-kklein 1ec8f5c
Merge remote-tracking branch 'origin/enum_member_docs' into enum_memb…
hf-kklein 3bcce58
extend comment
hf-kklein e816aea
ignore missing pylint type hints
hf-kklein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,14 @@ | ||
| # pylint: disable=missing-module-docstring | ||
| from bo4e.enum.strenum import StrEnum | ||
| from bo4e.enum.strenum import DocumentedStrEnum | ||
|
|
||
|
|
||
| class Anrede(StrEnum): | ||
| class Anrede(DocumentedStrEnum): | ||
| """ | ||
| Übersicht möglicher Anreden, z.B. eines Geschäftspartners. | ||
| """ | ||
|
|
||
| HERR = "HERR" | ||
| FRAU = "FRAU" | ||
| EHELEUTE = "EHELEUTE" | ||
| FIRMA = "FIRMA" | ||
| INDIVIDUELL = "INDIVIDUELL" | ||
| HERR = "HERR", "Herr" | ||
| FRAU = "FRAU", "Frau" | ||
| EHELEUTE = "EHELEUTE", "Eheleute" | ||
| FIRMA = "FIRMA", "Firma" | ||
| INDIVIDUELL = "INDIVIDUELL", 'Individuell (z.B. "Profx")' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import inspect | ||
| import re | ||
| from pathlib import Path | ||
| from typing import List, Optional, TypeVar | ||
|
|
||
| import pytest # type:ignore[import] | ||
|
|
||
| from bo4e.enum import anrede | ||
| from bo4e.enum.anrede import Anrede | ||
| from bo4e.enum.strenum import StrEnum | ||
|
|
||
|
|
||
| class TestEnums: | ||
| """ | ||
| A test class that checks the enum construction. | ||
| """ | ||
|
|
||
| starts_with_whitespace_pattern = re.compile(r"^[\s\n]+") | ||
| ends_with_whitespace_pattern = re.compile(r"[\s\n]+$") | ||
|
|
||
| TEnum = TypeVar("TEnum", bound=StrEnum) | ||
|
|
||
| @staticmethod | ||
| def _get_all_enum_classes() -> List[TEnum]: | ||
| """ | ||
| returns a list of all bo4e.enum classes | ||
| """ | ||
| arbitrary_enum_module_path = Path(anrede.__file__) | ||
| result = [] | ||
| for python_file in arbitrary_enum_module_path.parent.glob("*.py"): | ||
| # don't ask me why. but it works. | ||
| enum_module = __import__("bo4e.enum." + python_file.name.split(".")[0]) | ||
| for _, member in inspect.getmembers(enum_module.enum): | ||
| if inspect.ismodule(member): | ||
| candidate = inspect.getmembers(member)[0][1] | ||
| if inspect.isclass(candidate): | ||
| result.append(candidate) | ||
| return result | ||
|
|
||
| @staticmethod | ||
| def _get_class_doc(enum_class: TEnum) -> Optional[str]: | ||
| """ | ||
| asserts that enum class is a class and returns the class' docstring | ||
| """ | ||
| assert inspect.isclass(enum_class) | ||
| return inspect.getdoc(enum_class) | ||
|
|
||
| def test_enum_classes_docstrings(self): | ||
| """ | ||
| Tests that the docstrings of the enum classes do not start with whitespace or blank lines. | ||
| """ | ||
| all_enums = TestEnums._get_all_enum_classes() | ||
| assert len(all_enums) > 100 # just to be sure we're not using the wrong directory or path | ||
| for enum_class in all_enums: | ||
| docstring = TestEnums._get_class_doc(enum_class) | ||
| assert docstring is not None | ||
| assert not TestEnums.starts_with_whitespace_pattern.match(docstring) | ||
| assert not TestEnums.ends_with_whitespace_pattern.match(docstring) | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "enum_member, expected_docstring", | ||
| [ | ||
| pytest.param(Anrede.HERR, "Herr"), | ||
| pytest.param(Anrede.INDIVIDUELL, 'Individuell (z.B. "Profx")'), | ||
| ], | ||
| ) | ||
| def test_enum_member_docstrings_explicitly(self, enum_member: TEnum, expected_docstring: Optional[str]): | ||
| """ | ||
| Test the docstrings of the enum members explicitly. | ||
| if the general approach (using DocumentedStrEnum) works for single members, it will also work for all enums, | ||
| which are constructed similarly. | ||
| """ | ||
| assert inspect.getdoc(enum_member) == expected_docstring | ||
|
|
||
| def test_enum_members_are_all_documented(self): | ||
| """ | ||
| The class docstrings are enforced using pylint but the docstrings of enum members are not covered by pylint. | ||
| """ | ||
| all_enums = self._get_all_enum_classes() | ||
| for enum_class in all_enums: | ||
| class_docstring = TestEnums._get_class_doc(enum_class) | ||
| for enum_member in enum_class: | ||
| member_docstring = inspect.getdoc(enum_member) | ||
| assert ( | ||
| member_docstring != class_docstring | ||
| ), f"Docstring of Enum member {enum_member} must not be the same as the class docstring" | ||
| assert member_docstring is not None | ||
| assert member_docstring != "" | ||
| assert not TestEnums.starts_with_whitespace_pattern.match(member_docstring) | ||
| assert not TestEnums.ends_with_whitespace_pattern.match(member_docstring) | ||
| break # proof this works just for the wip anrede |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried it with
StrEnumbut it did not work =(It seems that it can not find the right ENUM if we use the StrEnum class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same for me