Git Product home page Git Product logo

Comments (4)

adamchainz avatar adamchainz commented on July 30, 2024 1

Ah os.path.sep is incomplete, we need to use altsep too, if not None: https://docs.python.org/3.10/library/os.html#os.altsep

would you like to make a PR changing that instead?

from django-upgrade.

adamchainz avatar adamchainz commented on July 30, 2024

looks_like_migrations_file is really simple:

@cached_property
def looks_like_migrations_file(self) -> bool:
return "migrations" in self.filename.split(os.path.sep)

Can you try and debug why it’s not returning True? Seems like it could be a Windows/Powershell issue.

from django-upgrade.

CLWilliam avatar CLWilliam commented on July 30, 2024

Hi Adam,

it may be indeed a combining PowerShell & git ls-files issue. git ls-files -- '*.py' outputs the following:

...
projects/migrations/0001_initial.py
projects/migrations/0002_auto_20200512_1030.py
projects/migrations/0238_auto_20200424_1249.py
projects/migrations/0239_auto_20200430_1526.py
projects/migrations/0240_auto_20200505_0954.py
projects/migrations/0241_auto_20200514_1155.py
...

When this is passed to looks_like_migrations_file, this results in the following:

ipdb> self.filename
'projects/migrations/0238_auto_20200424_1249.py'

ipdb> os.path.sep
'\\'

ipdb> self.filename.split(os.path.sep)
['projects/migrations/0238_auto_20200424_1249.py']

As the separator used by git ls-files (and git bash in general) does not match the os.path.sep.
replacing the '/' did the trick:

git ls-files -- '*.py' | %{django-upgrade --target-version 4.1 $_.replace('/','\\')}

Is this something to be added in the docs? Or added as an argument?
An example doc change is present here:
image
I've added this as PR-285 is this seems OK for you.
Otherwise I'd gladly add it as an argument (to be used instead of os.path.sep if one is given).

from django-upgrade.

CLWilliam avatar CLWilliam commented on July 30, 2024

Not sure if using os.sep and os.altsep is the best way to do this:
Splitting on two delimiters can be done with re.split, the pattern will be something like:

sep_re = rf"[{os.sep}{os.altsep or ''}]"
# or to keep it simple:
sep_re = r"[\\/]"

Not sure if getting the path seperator from os would have an added value here.
But if we're already going into regex land, maybe simplifying to the general approach in data.py would be a better solution:

migrations_re = re.compile(r"(^|[\\/])migrations([\\/])")
commands_re = re.compile( r"(^|[\\/])management[\\/]commands[\\/]")
...
    @cached_property
    def looks_like_command_file(self) -> bool:
        return commands_re.search(self.filename) is not None
...
    @cached_property
    def looks_like_migrations_file(self) -> bool:
        return migrations_re.search(self.filename) is not None
...

also simplifying the following part as marked above:

@cached_property
def looks_like_command_file(self) -> bool:
parts = self.filename.split(os.path.sep)
try:
i = parts.index("commands")
except ValueError:
return False
return i > 0 and i < (len(parts) - 1) and parts[i - 1] == "management"

Is this ok for you?

from django-upgrade.

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.