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

Don't subclass RecData for RecipeManager #125

Merged
merged 5 commits into from
Jun 23, 2020

Conversation

takluyver
Copy link
Collaborator

RecipeManager being a subclass of RecData was causing problems for the singleton-ish machinery: if the single RecData instance was set up before RecipeManager, code that needed a RecipeManager object could get a RecData object instead. This aims to fix that, by moving from inheritance to composition.

Of course, lots of code uses methods from the (former) parent class on a RecipeManager object. Rather than trying to find and modify every case of that, I've added a __getattr__ method so it will still work.

I've also understood & cleaned up the logic for the not-quite-singleton machinery in RecData, and replicated it for RecipeManager. The idea is to have one instance of each per SQLite database file (or per SQLalchemy database URL).

Copy link
Collaborator

@cydanil cydanil left a comment

Choose a reason for hiding this comment

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

These are fairly in-depth changes, but that part of the application is heavily used, and from using the changes and perusing the code, it looks good!
Additional thumbs-up for the clear comments in RecipeManager.__getattr__.

The only minor comment I have are to break the long lines no. 1965 and 1967 in gourmet/backends/db.py.

@@ -114,7 +114,15 @@ class DBObject:
# categories_table: id -> recipe_id, category_entry_id -> id
# ingredients_table: ingredient_id -> id, id -> recipe_id

class RecData (Pluggable, BaseException):
def db_url(file=None, custom_url=None):
Copy link
Collaborator

Choose a reason for hiding this comment

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

please add type annotations while you're at it

_instance_by_db_url = {}

@classmethod
def instance_for(cls, file=None, custom_url=None):
Copy link
Collaborator

Choose a reason for hiding this comment

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

please add type annotations to this method

custom_url=None):
return cls._instance_by_db_url[url]

def __init__ (self, file, url):
Copy link
Collaborator

Choose a reason for hiding this comment

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

please add type annotations to this method


def __init__ (self,*args,**kwargs):
@classmethod
def instance_for(cls, file=None, custom_url=None):
Copy link
Collaborator

Choose a reason for hiding this comment

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

please add type annotations to this method

@takluyver
Copy link
Collaborator Author

I kind of feel that the instance_for annotations are a net negative, because the signature that previously fitted on one line is now an ugly mess over three lines. 🤷

@maweki
Copy link
Collaborator

maweki commented Jun 23, 2020

I kind of feel that the instance_for annotations are a net negative, because the signature that previously fitted on one line is now an ugly mess over three lines. shrug

We will thank ourselves once we do any more complex refactoring. It looks good to me. Tank you.

@cydanil
Copy link
Collaborator

cydanil commented Jun 23, 2020

Thanks again :)

@cydanil cydanil merged commit 1b9b65e into kirienko:master Jun 23, 2020
@takluyver takluyver deleted the disinherit-recipemanager branch June 24, 2020 07:45
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.

3 participants