Issue2558

classification
Title: Feature request: Automatic string to enum coercion
Type: rfe Severity: minor
Components: Core Versions:
Milestone:
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: jamesmudd, jeff.allen, stefan.richthofer
Priority: low Keywords: RFE

Created on 2017-02-28.19:57:55 by jamesmudd, last changed 2018-03-12.20:22:00 by jeff.allen.

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.
msg11783 (view) Author: Jeff Allen (jeff.allen) Date: 2018-03-12.08:49:15
I think automatic coercion of a string would turn out badly: we'll discover cases where two signatures match. Compare the insoluble #1781.

We can do this:
>>> import java.time.LocalDate
>>> from java.time import Month
>>> java.time.LocalDate.of(2015, Month.DECEMBER, 3)
2015-12-03

or:
>>> from java.time.Month import *
>>> DECEMBER
DECEMBER
>>> java.time.LocalDate.of(2015, DECEMBER, 3)
2015-12-03

Does that not do the trick?
msg11784 (view) Author: James Mudd (jamesmudd) Date: 2018-03-12.12:49:23
Yes there are lots of better solutions than what is proposed here which is almost certainly a bad idea.

This should be closed as won't implement.
msg11785 (view) Author: Jeff Allen (jeff.allen) Date: 2018-03-12.20:22:00
Thanks James. Looking at GitHub, I see you had second thoughts (and third) nearly a year ago.
History
Date User Action Args
2018-03-12 20:22:00jeff.allensetstatus: open -> closed
resolution: remind -> rejected
messages: + msg11785
2018-03-12 12:49:23jamesmuddsetmessages: + msg11784
2018-03-12 08:49:16jeff.allensetkeywords: + RFE
type: behaviour -> rfe
messages: + msg11783
nosy: + jeff.allen
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