Implicit version comparison with strings
See original GitHub issueThis seems like a cool project and I’d like to include it in a project I’m working on. Is there any chance you’d be willing to support implicit comparison of Version
and Spec
objects with string types? For example, I’d like the following cases to work:
import semantic_version as sv
assert sv.Version('1.0.0') == '1.0.0'
spec = sv.Spec('>=2.0.0')
assert spec.match('2.1.0')
It’s less important, but I also think this would be useful with tuples:
assert sv.Version('1.0.0') == (1,0,0)
It doesn’t seem like this would be very difficult or unreasonable to support. I’d be willing to submit a PR myself.
Issue Analytics
- State:
- Created 6 years ago
- Comments:5
Top Results From Across the Web
Implicit CASTs for comparisons - IBM
Implicit CASTs for comparisons · Numeric types · Character strings · Datetime values · Booleans · Intervals · Comparing character strings with other ......
Read more >How do you compare two version Strings in Java?
Another solution for this old post (for those that it might help) : public class Version implements Comparable<Version> { private String version; ...
Read more >Best Practices for Comparing Strings in .NET - Microsoft Learn
Avoid the following practices when you compare strings: Do not use overloads that do not explicitly or implicitly specify the string comparison ......
Read more >12.8.1 String Comparison Functions and Operators
A number converted to a string is treated as a binary string. This affects only comparisons. Normally, if any expression in a string...
Read more >PCMPISTRM — Packed Compare Implicit Length Strings ...
Opcode/Instruction, Op/En, 64/32 bit Mode Support, CPUID Feature Flag, Description. 66 0F 3A 62 /r imm8 PCMPISTRM xmm1, xmm2/m128, imm8, RM, V/V, SSE4_2 ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I was to file a new issue, and smart github suggested me this one! Here is my arguments for support of comparisons against strings:
Python’s distutils.version supports it (or both left and right comparison), e.g.
so to migrate code which used
distutils.version
classes to use proper semantic versioning via this package would require explicit casting into target class at the point of comparison, which would require more code refactoring. Moreover it might make it harder in some use cases to use generic code, such assome_version_instance < '1.0.0'
wheresome_version_instance
could be eithersemantic_version.Version
ordistutils.version.StrictVersion
instance depending on logic somewhere else in the code on what kind of versioning scheme is used.You make a fair point, but at some point the contents of any variable are only meaningful in context right? If I say
a = 5
andb = 5
, is comparison ofa == b
meaningful and/or useful to me or not? Maybea
represents length andb
represents dollars. On the other hand, if I choose to compareVersion('1.0.0') == '1.0.0'
, then I have already declared that the results of this comparison are meaningful to me.For me it boils down to not having to track down every single case in my code where I would have to explicitly convert something to
Version
in order to be able to perform a meaningful and obvious comparison. But this is a problem I have already solved in my own code.And yes discussion is useful 😃. I’m not sure if we’re getting anywhere in this case, however.