Repository navigation
Don't convert value if it is already a valid type. - #3895
Conversation
d5483a6 to
5af3d2c
Compare
Codecov Report
@@ Coverage Diff @@
## master #3895 +/- ##
==========================================
- Coverage 75.22% 75.16% -0.05%
==========================================
Files 222 222
Lines 18789 18791 +2
Branches 3070 3071 +1
==========================================
- Hits 14132 14123 -9
- Misses 4128 4137 +9
- Partials 529 531 +2
Continue to review full report at Codecov.
|
d4642c2 to
4451ba0
Compare
|
The current functionality is there by design. The motivation is that we want conversion to be done if you have type like |
|
The current functionality breaks optional strings If the purpose of this is to allow int/float/enum/None literals to override def _no_conversion_needed(self, value):
if isinstance(value, str):
return False
# None is not a valid type, convert to NoneType
valid_instance_tuple = tuple(type(it) if it is None else it for it in self.args)
return isinstance(value, valid_instance_tuple)Or just always put def _convert(self, value, explicit_type=True):
if str in self.args:
args = tuple(arg for arg in self.args if arg != str) + str
else:
args = self.args
for typ in args:
converter = TypeConverter.converter_for(typ)
if not converter:
return value
try:
return converter.convert('', value, explicit_type)
except ValueError:
pass
raise ValueError |
4451ba0 to
78887b7
Compare
|
How does the current functionality break |
|
|
|
issue: #3897 |
|
I agree |
|
After thinking this more, I believe the approach this PR originally used is the best one. See my comment to issue #3897 for more details. I also submitted a separate issue about the problem with |
| if isinstance(value, str): | ||
| return False | ||
| # None is not a valid type, convert to NoneType | ||
| valid_instance_tuple = tuple(type(it) if it is None else it for it in self.args) |
There was a problem hiding this comment.
This could be written like
NoneType = type(None)
types = tuple(typ if typ is not None else NoneType for typ in self.args)to avoid the need for the comment.
73eb67b to
5fd1f93
Compare
5fd1f93 to
1c5d9ee
Compare
|
Looks great. Docs related to Union conversion need to still be updated but I can do that myself. I'll also make sure we have tests covering the |
|
One of the tests was actually failing. I should've checked CI status more before merging, but I was anyway planning to enhance tests a bit so I'll fix the test at the same time. |
#3867
#3738
If a value is already a valid type from a type union then there is no need to convert it.