-
-
Notifications
You must be signed in to change notification settings - Fork 457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix overloads and remove PathLike
in finders
#1063
Conversation
Signed-off-by: Anders Kaseorg <[email protected]>
Django really requires these paths to be str. For example, in FileSystemFinder, .find(path) calls .find_location(…, path, …) which evaluates path.startswith(prefix) and path[len(prefix) :]; these don’t work on arbitrary PathLike objects. Signed-off-by: Anders Kaseorg <[email protected]>
Here's the function we are talking about: https://github.com/django/django/blob/3.2/django/contrib/staticfiles/finders.py#L110-L122 def find_location(self, root, path, prefix=None):
"""
Find a requested static file in a location and return the found
absolute path (or ``None`` if no match).
"""
if prefix:
prefix = '%s%s' % (prefix, os.sep)
if not path.startswith(prefix):
return None
path = path[len(prefix):]
path = safe_join(root, path)
if os.path.exists(path):
return It looks like |
The |
This does look like I possibly overlooked that string interpolation (and that means it also wouldn't work with bytes). I was very likely trying to address I'll test this branch against our codebase but we don't use paths that much, I just noticed that other places were using paths and I thought I had reviewed the changes close enough and figured the tests would catch any other issues. |
Now that I'm looking at the code a little more, and happened to look at a particular line, I believe I was mainly trying to get Still curious why mypy didn't point out that I made things too wide. |
Mypy isn’t being run against the code of Django itself. There’s no automated check that any of the typing claims in django-stubs reflect reality. |
I thought it was, is it only the tests? |
We type-check our own tests ( |
I don't believe that's correct, the script in I'll look at seeing what it might take to have stubtest run against Django's source since I'm not completely sure why we're running mypy against the tests since where I first realized we were doing this was in https://github.com/typeddjango/djangorestframework-stubs and that was a bit brittle because we don't supply the stubs anywhere. Since this project is the stubs for Django it makes sense to run stubtest against it. Running it quickly right now I'm seeing that there's some errors in the stub files and some declarations and not types are defined. This may be a result of a bug in stubgen, but it also may just have been that someone added a buggy stub that mypy will normally ignore the error, but stubtest is pickier since it's supposed to be testing stubs for validity. |
Either way that explains why mypy wasn't catching this issue and this PR should be clear for merging and I can have a follow up PR if stubtest makes sense to add for the CI checks. |
Ah, I see. But I don’t think it’d help much to try to expand that from the tests to the whole source. There’s no way to type-check upstream |
Interesting, I didn't realize that didn't work. Thanks for pointing me at the other issue. I figured that would work. I've played with retype before, and stubtest didn't seem to be doing what I wanted / producing too many errors for this project. It does look like the only way to get what I am trying to do is to use retype to put the types back into place and then use mypy to check the types as I would have it expected it would have when being handed both the stubs and the source. I was having some issues with retype when I had played with it before and it's possible this all might be a bit futile at this point. I might have more time at a later date, but right now I've already poked at this more than I should have 😜. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to make it clear I'm cool with these changes 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
I have made things!
Django really requires these paths to be
str
. For example, inFileSystemFinder
,.find(path)
calls.find_location(…, path, …)
which evaluatespath.startswith(prefix)
andpath[len(prefix) :]
; these don’t work on arbitraryPathLike
objects.Related issues