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 Python support for set() function #1893

Closed
EmilyBourne opened this issue May 17, 2024 · 1 comment · Fixed by #1901
Closed

Add Python support for set() function #1893

EmilyBourne opened this issue May 17, 2024 · 1 comment · Fixed by #1901
Assignees
Labels
Containers tuples/lists/sets/maps enhancement

Comments

@EmilyBourne
Copy link
Member

Describe the feature

I would like to be able to use the set() function. This is important so that empty sets can be created. The set() function is different to the {,} function as it takes an iterable.

Test Code

Provide code which does not currently work but which should do when this issue is fixed:

if __name__ == '__main__':
    a = set([1,2,3])
    b = [4,5,6]
    c = set(b)
    d = set(c)

Proposed Solution

This feature should be implemented similarly to what was done for tuples. The equivalent issues for tuples was resolved in #1589.
The pertinent code is:

class PythonTupleFunction(TypedAstNode):
"""
Class representing a call to the `tuple` function.
Class representing a call to the `tuple` function. This is
different to the `(,)` syntax as it only takes one argument
and unpacks any variables.
Parameters
----------
arg : TypedAstNode
The argument passed to the function call.
"""
__slots__ = ()
_attribute_nodes = ()
def __new__(cls, arg):
if isinstance(arg, PythonTuple):
return arg
elif isinstance(arg, (PythonList, InhomogeneousTupleVariable)):
return PythonTuple(*arg)
elif isinstance(arg.shape[0], LiteralInteger):
return PythonTuple(*[arg[i] for i in range(arg.shape[0])])
else:
raise TypeError(f"Can't unpack {arg} into a tuple")

Additional context

It is important to handle the set function so that this function can be used for type annotations. If the {,} function is used bugs such as #1892 can arise.

@EmilyBourne EmilyBourne added enhancement Containers tuples/lists/sets/maps labels May 17, 2024
@EmilyBourne
Copy link
Member Author

cc: @mustapha-belbiad

@EmilyBourne EmilyBourne changed the title Add support for set() function Add Python support for set() function May 17, 2024
@mustapha-belbiad mustapha-belbiad self-assigned this May 24, 2024
EmilyBourne pushed a commit that referenced this issue Jun 7, 2024
Add support for the `set()` function in Python. This fixes #1893. 
In this PR, the `PythonSetFunction` class is introduced to handle calls
to the `set()` function, along with the addition of related tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Containers tuples/lists/sets/maps enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants