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

add classmethod constructors #6

Merged
merged 1 commit into from
Oct 2, 2023
Merged

Conversation

beheh
Copy link
Contributor

@beheh beheh commented Sep 25, 2023

This PR moves the implementation of from_string and from_uuid to classmethods of the same name.

That means instead of

from typeid import from_string

the_id = from_string(...)

...you can now write:

from typeid import TypeID

the_id = TypeID.from_string(...)

I've kept the old methods around for now, but they defer to the new classmethods as the source implementation. If you're onboard with this change you could also consider dropping them, but they probably don't hurt and it would be a breaking change.

Motivation

  • It's a common pattern in the Python world ("Also, see classmethod() for a variant that is useful for creating alternate class constructors."1)
  • It will often reduce the number of imports from the typeid package, because it's likely you'll already import TypeID anyway for e.g. function param or return typing (def find_by_id(id: TypeID)).
  • Especially from_string is a fairly ambiguous name, and importing it in a large file might often need an alias
  • When dealing in a big project it's easier to remember a generic import (TypeID) rather than the constructors for each library ("Was it from_uuid or get_by_uuid...?")
  • It follows the TypeScript TypeID library which uses TypeID.fromString and TypeID.fromUUID

Footnotes

  1. https://docs.python.org/3/library/functions.html#staticmethod

@akhundMurad
Copy link
Owner

Great job! Will push it with another PR.

@akhundMurad akhundMurad merged commit 7a02841 into akhundMurad:main Oct 2, 2023
3 checks passed
@beheh beheh deleted the constructors branch October 2, 2023 13:16
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