Issue2558

classification
Title: Feature request: Automatic string to enum coercion
Type: behaviour Severity: minor
Components: Core Versions:
Milestone:
process
Status: open Resolution: remind
Dependencies: Superseder:
Assigned To: Nosy List: jamesmudd, stefan.richthofer
Priority: low Keywords:

Created on 2017-02-28.19:57:55 by jamesmudd, last changed 2017-03-03.11:15:55 by stefan.richthofer.

Messages
msg11142 (view) Author: James Mudd (jamesmudd) Date: 2017-02-28.19:57:54
This is not a bug, but i wasn't sure where to put improvements? It would be nice if when calling a java method which takes a enum automatic type conversion from string could be done if possible. This would make calling enum methods more convenient.

At the moment a call to a method taking a enum with a string fails, even if the enum valueOf(String) method would work e.g

Jython 2.7.1b3 (, Feb 28 2017, 19:53:14) 
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_121
Type "help", "copyright", "credits" or "license" for more information.
>>> import java.time.LocalDate
>>> date = java.time.LocalDate.of(2015, 'DECEMBER', 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: of(): 2nd arg can't be coerced to int, java.time.Month
>>> 

Since Jython already does other similar type coercion I think this would be a nice extension, but maybe i'm missing a reason why its a really bad idea?
msg11143 (view) Author: James Mudd (jamesmudd) Date: 2017-02-28.20:02:04
I have a pull request which can enable this https://github.com/jythontools/jython/pull/58

After this change the behaviour is

Jython 2.7.1b3 (, Feb 28 2017, 19:05:08) 
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_121
Type "help", "copyright", "credits" or "license" for more information.
>>> import java.time.LocalDate
>>> date = java.time.LocalDate.of(2015, 'DECEMBER', 3)
>>> date
2015-12-03
>>> date.getMonth().__class__
<type 'java.time.Month'>
>>> 

Where 'DECEMBER' is automatically coerced into java.time.Month.DECEMBER

I would be interested in what people think of this idea?
msg11160 (view) Author: Stefan Richthofer (stefan.richthofer) Date: 2017-03-03.11:13:08
I am actually much for improving Java integration, but IMO Strings and Enums are quite not the same (and don't even share an is-relation in any direction). Looking at https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html an Enum can also be a Planet etc, have custom properties, even methods.

Note that enumMethod in your test also wouldn't accept a String when called in Java-code.

You suggest to let Jython convert silently a String in the role of an element name into a value of Enum subtype. For me this looks like mixing up something like a lookup (e.g. in a map) with an is-relation. Or stated differently: Too much magic, even for Jython users.

That said, I would like to have a proper way to allow the method call you mention (maybe there already is?)
E.g. something like an explicit (is better than implicit...^^) way to let Jython perform Enum-lookup from Python-code.
Maybe we could have an integration with the official Python module for enums https://pypi.python.org/pypi/enum34 (for Python 3, but also backported to several Python 2 versions).

I will think about it and would also wish to hear more opinions.
History
Date User Action Args
2017-03-03 11:15:55stefan.richthofersetpriority: low
resolution: remind
title: Automatic string to enum coercion -> Feature request: Automatic string to enum coercion
2017-03-03 11:13:10stefan.richthofersetnosy: + stefan.richthofer
messages: + msg11160
2017-02-28 20:02:04jamesmuddsetmessages: + msg11143
2017-02-28 19:57:55jamesmuddcreate