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

[stlib] String conversion of list literals and lists with representable items #3521

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

msaelices
Copy link
Contributor

@msaelices msaelices commented Sep 21, 2024

To improve the ergonomics of the Mojo language for Python developers:

l = [1, False, "Mojo is awesome 🔥"]
print(l)
# [1, False, 'Mojo is awesome 🔥']
l = List(1, 2, 3)
print(str(l))
# [1, 2, 3]

To improve the ergonomics of the Mojo language for Python developers.

Signed-off-by: Manuel Saelices <[email protected]>
@msaelices msaelices requested a review from a team as a code owner September 21, 2024 22:15
@msaelices msaelices changed the title [stlib] String conversion of list literals. [stlib] String conversion of list literals and lists Sep 21, 2024
@msaelices msaelices changed the title [stlib] String conversion of list literals and lists [stlib] String conversion of list literals and lists with representable items Sep 21, 2024
Copy link
Collaborator

@JoeLoser JoeLoser left a comment

Choose a reason for hiding this comment

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

This is great, thank you!

@@ -16,7 +16,10 @@ from testing import assert_equal


def test_str_none():
assert_equal(str(None), "None")
# TODO(#3393): Change to str(None) when MLIR types do not confuse overload resolution.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Question Is this still valid since we have NoneType in the stdlib now? We could give the conformance of RepresentableCollectionElement to it and the previous code may "just work"?

writer.write(", ")

@parameter
if _type_is_eq[T, PythonObject]():
Copy link
Collaborator

Choose a reason for hiding this comment

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

@jackos would this sort of thing able to be improved with your in-flight PR for writer? I don't love explicitly enumerating a bunch of types that we know are convertible to writable since we just store a Tuple[*Ts] and don't know if the Ts are writable themselves.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe we need Mojo properly condicional conformance support, or at least to have a _implements() logic (see #3630) to be able to make this work without enumerating a bunch of types. This is the only workaround I know, and this is also used by @martinvuyk in the DType.get_type() implementation (see #3810)

@skongum02 skongum02 deleted the branch modular:main January 29, 2025 18:58
@skongum02 skongum02 closed this Jan 29, 2025
@skongum02 skongum02 reopened this Jan 29, 2025
@skongum02 skongum02 changed the base branch from nightly to main January 29, 2025 20:54
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