Git Product home page Git Product logo

Comments (7)

socketpair avatar socketpair commented on August 24, 2024 1

@Pierre-Sassoulas I would call useless-f-string

from pylint.

Pierre-Sassoulas avatar Pierre-Sassoulas commented on August 24, 2024

+1 from me. It also seems relatively immune to false positive and simple without looking too much into it. How would you name it ? useless-string, useless-fstring-formatting useless-format-string ?

from pylint.

DanielNoord avatar DanielNoord commented on August 24, 2024
class SneakyString(str):
    def __str__(self) -> str:
        return "Different string"


x = SneakyString("A")

print(f"{x}" + "B")
print(f"{x:.2}" + "B")
print(x + "B")

I think there is a actually a pretty high chance for false positives as the logic for f-strings is quite difficult to statically imitate.

from pylint.

Pierre-Sassoulas avatar Pierre-Sassoulas commented on August 24, 2024

Not sure what the false positive is in this case @DanielNoord ? Isn't print(f"{x}" + "B") an actual issue ? As it should be print(str(x) + "B") to be equivalent (well, print(f"{x}B"), but this would become hard). I imagine the message raised should be something like Useless f-string, use 'x' or str(x)' instead of 'f"{x}"'.

from pylint.

DanielNoord avatar DanielNoord commented on August 24, 2024

The false positive is that f"{x}" isn't the same as x. f"{x}" is probably quicker than str(x) as there is no lookup for the str() function and thus suggesting str(x) instead of f"{x}" is actually not in line with some of our other checks 😅

from pylint.

socketpair avatar socketpair commented on August 24, 2024

@DanielNoord

In [1]: timeit f'{42}'
70 ns ± 1.14 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

In [2]: timeit str(42)
71.9 ns ± 0.856 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

from pylint.

Pierre-Sassoulas avatar Pierre-Sassoulas commented on August 24, 2024

Right. Still + 0.5 on this, but it's less useful if it become consider-f-string-uselessness and a convention check, for sure.

from pylint.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.