tldr: I have consumer-like classes that require a number of pieces of information to do their job. It's an "all or nothing" kind of thing: the "producers" providing them with data need to provide all necessary pieces of information or there will be a problem. I want to provide some assertions to make sure everything is present. I know that python is normally a fan of duck-typing, but because I need to have all data present in all cases, I feel it would be easier to make a base class that the producers have to implement, and then assert via
instanceof. Is this a reasonable choice or is the preference still for duck-typing?
I figure some background is good to make sure I'm not falling victim to the XY problem. I'm working on a system to manage database structure via version control (a database migration system). I have some classes that compare table definitions to see if anything has changed. The table definitions themselves can come from a variety of locations: it can come from the definitions stored in text files, it can come from reading the actual database, etc... So I have some classes responsible for loading up the database structure from various sources (producers), and then other classes who take those definitions and look for differences (consumers).
I briefly considered having a third entity involved to store the actual table definition, and which would be built by the producers and passed to the consumers. However, I don't think there would actually need to be any behavior associated with such a class: it would just operate as a dumb data carrier between the two which would just have a few properties on it. As a result, I think I would rather skip it, since it has no real responsibilities.
Instead (in the lingo of other programming languages), my thought was to have a simple interface that defines that table definition. The producer classes would implement the interface, read their respective sources, and populate the data accordingly. The consumers would then simply accept any object that implements the table definition interface. Obviously though, this is not the python way.
Having done some reading on the topic, it seems like duck typing is the preferred go-to solution for this problem. In my case though, I feel like it may not be the best fit. The biggest reason is because I don't foresee any need for flexibility in the required data parameters. If I always need the same half-dozen or so properties, I feel like it would violate DRY to check that all parameters are present everytime I need them.
Rather, my thought is to let the table definition be a base class which the producers then add to their inheritance chain, and which defines the properties which must be provided. The consumers will then just do a simple
instanceof assertion before doing their thing.
Is that a reasonable approach to this problem in python, or are there preferred ways?