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

Improve argument singletons #1760

Merged
merged 20 commits into from
Mar 1, 2024
Merged

Conversation

EmilyBourne
Copy link
Member

@EmilyBourne EmilyBourne commented Feb 23, 2024

The class ArgumentSingleton currently lets us create singleton classes such that there is only one instance of the class for any given set of arguments. However our current implementation relies on *args, **kwargs to guarantee compatibility with the __init__ function of the class. This results in functools.signature reporting the wrong signature. As a result numpydoc insists on docstrings matching the prototype of ArgumentSingleton.__call__. This is manageable when there are few classes using the metaclass (as is the case currently), but it is unhelpful when the class is used more widely (as is the case in #1756). The solution to this is to use the __signature__ property documented in inspect.signature to trick numpydoc into seeing the signature of the __init__ function.

@pyccel-bot
Copy link

pyccel-bot bot commented Feb 23, 2024

Hello again! Thank you for this new pull request 🤩.

Please begin by requesting your checklist using the command /bot checklist

@EmilyBourne
Copy link
Member Author

EmilyBourne commented Feb 23, 2024

Here is your checklist. Please tick items off when you have completed them or determined that they are not necessary for this pull request:

  • Write a clear PR description
  • Add tests to check your code works as expected
  • Update documentation if necessary
  • Update Changelog
  • Ensure any relevant issues are linked
  • Ensure new tests are passing

@EmilyBourne EmilyBourne marked this pull request as ready for review February 23, 2024 11:51
@github-actions github-actions bot marked this pull request as draft February 23, 2024 11:53
@EmilyBourne EmilyBourne marked this pull request as ready for review February 23, 2024 12:06
@github-actions github-actions bot marked this pull request as draft February 23, 2024 12:13
@pyccel-bot
Copy link

pyccel-bot bot commented Feb 23, 2024

Unfortunately your PR is not passing the tests so it is not quite ready for review yet. Let me know when it is fixed with /bot mark as ready.

@EmilyBourne EmilyBourne marked this pull request as ready for review February 23, 2024 14:06
Copy link

@pyccel-bot pyccel-bot bot left a comment

Choose a reason for hiding this comment

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

Good job ! Your PR is using all the code it added/changed.

@pyccel-bot
Copy link

pyccel-bot bot commented Feb 23, 2024

Hey @pyccel/pyccel-dev ! @EmilyBourne has just created this great new pull request! Check it out and let me know what you think!

Copy link
Contributor

@mustapha-belbiad mustapha-belbiad left a comment

Choose a reason for hiding this comment

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

Looks good to me

@pyccel-bot pyccel-bot bot added Ready_for_review Received at least one approval. Requires review from senior developer and removed needs_initial_review labels Feb 23, 2024
@pyccel-bot
Copy link

pyccel-bot bot commented Feb 23, 2024

Hey @yguclu, @EmilyBourne, this PR is looking pretty good. @EmilyBourne and @mustapha-belbiad think it is ready to merge. Could you add your expertise to confirm that this follows all the coding conventions and fits in Pyccel's future plans? Thanks 😄

@EmilyBourne EmilyBourne marked this pull request as draft March 1, 2024 08:20
@EmilyBourne EmilyBourne removed the Ready_for_review Received at least one approval. Requires review from senior developer label Mar 1, 2024
@EmilyBourne EmilyBourne removed the request for review from yguclu March 1, 2024 08:20
@EmilyBourne EmilyBourne dismissed mustapha-belbiad’s stale review March 1, 2024 08:21

Rewrite coming. Review will no longer be relevant

@EmilyBourne EmilyBourne marked this pull request as ready for review March 1, 2024 09:32
Copy link

@pyccel-bot pyccel-bot bot left a comment

Choose a reason for hiding this comment

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

Good job ! Your PR is using all the code it added/changed.

@pyccel-bot
Copy link

pyccel-bot bot commented Mar 1, 2024

Hey @pyccel/pyccel-dev ! @EmilyBourne has just created this great new pull request! Check it out and let me know what you think!

@pyccel-bot pyccel-bot bot requested a review from a team March 1, 2024 09:52
bases : tuple[class,...]
A tuple of the superclasses of the class.
dct : dict
A dictionary of the class attributes.
"""
_instances = {}
Copy link
Member

@yguclu yguclu Mar 1, 2024

Choose a reason for hiding this comment

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

I find it a bit confusing to give an _instances attribute to the metaclass. This will mean that the dictionary is in the metaclass rather than in each of the actual argument-singleton types. Why don't we create the dictionary in the __init__ instead?

class ArgumentSingleton(type):

    def __init__(cls, name, bases, dct):
        cls._instances = {}  # Dictionary local to each argument-singleton type
        cls.__signature__ = signature(cls.__init__)
        super().__init__(name, bases, dct)

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree. I just kept the old implementation without thinking about it. Fixed: 4c0369d

Copy link
Member

Choose a reason for hiding this comment

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

If the dictionary is local to each argument-singleton type, it sounds like there is no need to include cls in the dictionary key. What do you think?

Copy link
Member

@yguclu yguclu Mar 1, 2024

Choose a reason for hiding this comment

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

If my last comment is correct, then we have no need to use a dictionary in the "simple" singleton case. And in fact it would be beneficial not to have the metaclass Singleton subclass ArgumentSingleton, in order to simplify the code. E.g. see this code.

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree. I have removed it: b273ef9

The original reason for making Singleton subclass ArgumentSingleton was to allow a class (DataType) to have the general metaclass (ArgumentSingleton), and the subclasses to redefine it (which is only allowed if the new metaclass is a subclass). But most of the time we want Singleton anyway and I am not sure that this is really helpful. I have split them back up again. 3405e0f

@EmilyBourne
Copy link
Member Author

/bot run pr_tests

Copy link

@pyccel-bot pyccel-bot bot left a comment

Choose a reason for hiding this comment

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

Good job ! Your PR is using all the code it added/changed.

@pyccel-bot
Copy link

pyccel-bot bot commented Mar 1, 2024

Hey @pyccel/pyccel-dev ! @EmilyBourne has just created this great new pull request! Check it out and let me know what you think!

@pyccel-bot pyccel-bot bot requested a review from a team March 1, 2024 11:56
@github-actions github-actions bot marked this pull request as draft March 1, 2024 12:04
@EmilyBourne EmilyBourne marked this pull request as ready for review March 1, 2024 12:08
@github-actions github-actions bot marked this pull request as draft March 1, 2024 12:08
@EmilyBourne EmilyBourne marked this pull request as ready for review March 1, 2024 12:11
Copy link

@pyccel-bot pyccel-bot bot left a comment

Choose a reason for hiding this comment

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

Good job ! Your PR is using all the code it added/changed.

@pyccel-bot
Copy link

pyccel-bot bot commented Mar 1, 2024

Hey @pyccel/pyccel-dev ! @EmilyBourne has just created this great new pull request! Check it out and let me know what you think!

@pyccel-bot pyccel-bot bot added Ready_to_merge Approved by senior developer. Ready for final approval and merge and removed needs_initial_review labels Mar 1, 2024
@yguclu yguclu merged commit 9c5d09b into devel Mar 1, 2024
12 checks passed
@yguclu yguclu deleted the ebourne_improved_argument_singleton branch March 1, 2024 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Ready_to_merge Approved by senior developer. Ready for final approval and merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants