question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implicit version comparison with strings

See original GitHub issue

This 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:open
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
yarikopticcommented, Apr 9, 2020

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.

$> python -c 'from distutils.version import LooseVersion as LV, StrictVersion as SV; print("0.9" < SV("0.10"))'      
True

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 as some_version_instance < '1.0.0' where some_version_instance could be either semantic_version.Version or distutils.version.StrictVersion instance depending on logic somewhere else in the code on what kind of versioning scheme is used.

0reactions
drdavellacommented, Jan 19, 2018

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 and b = 5, is comparison of a == b meaningful and/or useful to me or not? Maybe a represents length and b represents dollars. On the other hand, if I choose to compare Version('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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found