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

Type hints and cleanup of Barcode.build() and surrounding code #230

Merged
merged 11 commits into from
Jul 30, 2024

Conversation

maresb
Copy link
Contributor

@maresb maresb commented Apr 28, 2024

This is a follow-up to #228. I'm trying to not break backwards-compatibility for now, but just to make the code more understandable.

@maresb
Copy link
Contributor Author

maresb commented Apr 29, 2024

There's so much to do here. I'm grinding away, fixing stuff that catches my eye. If you let me know what you like and don't like, I can eventually try and rebase out the good parts.

Copy link
Owner

@WhyNotHugo WhyNotHugo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the slow reply, these were a lot of changes and needed serious attention.

The grand majority of this is fine; I only see one problem (which breaks existing usages and tests).

This also makes mypy fail for barcode/writer.py, but I can address that myself separately (you're only surfacing an existing issus).

barcode/base.py Outdated

def __repr__(self) -> str:
return f"<{self.__class__.__name__}({self.get_fullcode()!r})>"

def build(self) -> list[str]:
"""Return a singleton list with a string encoding the barcode as 1s and 0s."""
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: this isn't a singleton (a class which allows only one instance), it's a list with a single item (completely different things).

I think that some barcodes have other values (like G) to encode guard lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I'm using math terminology here. Thanks for pointing out the conflicting meaning, I'll change it!

I think that some barcodes have other values (like G) to encode guard lines.

Oh, ya, I see that now in EAN13, thanks!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I didn't know about the mathematics concept. I've learnt something today :)

return get_barcode(name)


def generate(
name: str,
code: str,
writer: BaseWriter | None = None,
output: str | (os.PathLike | (BinaryIO | None)) = None,
writer: BaseWriter | None,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This parameter was previously optional. With this change, it is no longer optional.

Some tests fail because of this. You can run them with pytest.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm changing this back.

I'm not defending it, but for disclosure I think for some reason I wanted output to never not be None. This forced me to remove the default value. Since positional args can't come after kwargs, this forced me to remove the default value on writer.

@@ -204,65 +219,60 @@ def packed(self, line: str) -> Generator[tuple[int, float], str, None]:
yield (-c, self.guard_height_factor)
c = 1

def render(self, code):
def render(self, code: list[str]) -> Any:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use Any; just leave the type hint absent until we can address it.

@maresb
Copy link
Contributor Author

maresb commented Jul 8, 2024

Sorry for the slow reply, these were a lot of changes and needed serious attention.

Ya, honestly I felt bad for dumping this mess on you. Thanks so much for sifting through it!!! This looks quick, let me see if I can crank this out now...

@maresb
Copy link
Contributor Author

maresb commented Jul 8, 2024

I got pytest running. Now I'm looking into:

>       raise RuntimeError(
            f"Character {char} could not be converted in charset {self._charset}."
        )
E       RuntimeError: Character 9 could not be converted in charset C.

@maresb
Copy link
Contributor Author

maresb commented Jul 8, 2024

Ok, cool, I introduced this exception in 5ff0cd7 😂

tests/test_manually.py Outdated Show resolved Hide resolved
@maresb
Copy link
Contributor Author

maresb commented Jul 8, 2024

@WhyNotHugo, could you please re-review? I think I've addressed your concerns. Also, I tried to make the individual commits very simple.

One potentially confusing commit is "Parametrize test_generating_barcodes". I preserved the HTML by creating a module-scoped fixture that collects the individual HTML image elements. This way the test cases can pass or fail independently. (Split out into #233)

The other commit that requires some thought is "Cleanup Barcode.build()". The logic here is that only one invocation of self._convert can possibly encode a digit from charset C which results in returning None. Thus I split it out this case into a separate function _convert_or_buffer. While it's more lines-of-code, I think it's easier to understand being more type-friendly and less like a mysterious finite-state automaton.

@maresb
Copy link
Contributor Author

maresb commented Jul 8, 2024

If it's too much then let me know and I'll split it into multiple PRs.

@maresb maresb force-pushed the cleanup-build branch 2 times, most recently from 0f9c80f to 0c671cd Compare July 9, 2024 13:06
@WhyNotHugo
Copy link
Owner

I've triggered a rebase after merging #233.

Feel free to rebase locally and force-push if you had any pending changes.

@maresb
Copy link
Contributor Author

maresb commented Jul 9, 2024

Thanks @WhyNotHugo!!! I agree with the rebase.

I split off the non-minimal changes into #234, so the only unreviewed commits are the last four, directly addressing your feedback above, and I'd expect those four to be super-quick to review.

@maresb
Copy link
Contributor Author

maresb commented Jul 20, 2024

@WhyNotHugo, there is no urgency whatsoever on this, but it seems like you reviewed all but the last four small commits, so perhaps this is simple to merge.

@WhyNotHugo
Copy link
Owner

I think this is okay to merge. There are commits that make changes and later commits that revert them. Do you want to rebase this? Otherwise I'll just squash them into a single commit.

@maresb maresb changed the title Cleanup Barcode.build() Type hints and cleanup of Barcode.build() and surrounding code Jul 29, 2024
@maresb
Copy link
Contributor Author

maresb commented Jul 29, 2024

Let's squash. Thanks @WhyNotHugo!!!

@WhyNotHugo WhyNotHugo merged commit 980f3ff into WhyNotHugo:main Jul 30, 2024
13 checks passed
@WhyNotHugo
Copy link
Owner

Thanks!

@maresb maresb deleted the cleanup-build branch July 30, 2024 09:55
@maresb maresb restored the cleanup-build branch July 30, 2024 09:55
@maresb maresb deleted the cleanup-build branch July 31, 2024 11:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants