See Ash Code logo See Ash Code

The abstraction stopped making the converters easier to change

Ashlynn Antrobus
• • 3 min read

My PDF and DOCX converters had a shared parent class. So did their text and image extractors. The names lined up neatly: BookConversion, TextExtraction, and ImageExtraction.

The files they processed didn’t line up quite so neatly.

A DOCX text extractor works on a paragraph. A PDF text extractor works on a page of layout elements and can return several pieces of text. Both extract text, but that description leaves out much of what the calling code needs to know.

Two dragons work on a picture and a notebook at neighboring tables, with a pot of brushes between them.

Illustration: different kinds of work can still share tools.

In December, I removed the inheritance from those concrete converters and extractors. A January test change followed the new dependency.

What the parent supplied

BookConversion held the input path, metadata, text extractor, and some shared settings. It started reading the file through _read_file, a method each child had to provide. It also required methods for parsing, writing text, and returning a string.

That saved some repeated setup. But understanding a concrete converter meant looking through the child and its parent to find where its input stream and settings came from.

After the change, the DOCX converter initializes its paragraph generator directly:

self.paragraphs: Generator[Paragraph, None, None] = self._read_file(
    file_path
)
self.metadata = metadata
self.text_extractor = text_extractor

The PDF converter initializes its pages in its own constructor. Each class now describes the file objects it processes, without using the parent’s _objects attribute to stand for both kinds.

There is a cost. Some assignments and settings appear in more than one class. I traded that duplication for having the setup beside the code that depends on it.

Sharing an operation without sharing a parent

The converters still need the same punctuation conversion utility. The revised DOCX class calls desmarten_text through its own small method rather than inheriting clean_text from BookConversion.

The text extractors still receive image extractors. Their constructor annotations now name the corresponding concrete extractor, such as PDFImageExtractor.

The resulting relationships can be sketched like this:

PDFConverter -> PDFTextExtractor -> PDFImageExtractor
DocxConverter -> DocxTextExtractor -> DocxImageExtractor

Those arrows describe collaborators. They don’t say that one converter is a kind of another, or that a DOCX paragraph and a PDF page have the same interface.

That choice also narrows the type relationship. Naming a concrete class can make substitution less flexible for a type checker. Removing an abstraction doesn’t automatically improve every part of the design.

The tests had a dependency too

In January, I removed the abstract image-extractor import from the PDF tests. Their fake extractor changed its parent from ImageExtraction to PDFImageExtractor while keeping its replacement extract_images method.

That commit updates a test dependency; despite its short commit title, it isn’t a deletion of the entire abstract-class module. It is another place the previous relationship had to be considered.

The converters could describe their own setup and still call the same helpers. I’d already made a similar change to where the paragraph lived in the earlier state refactor.

AA

Written by Ashlynn Antrobus

Software developer, author, and builder. Sharing technical lessons learned from building tools like Ebook2Text, IndexedDict, and rag-mcp, and navigating life as a programmer with dyslexia.