Skip to content

Fix name __path__ not defined #5212

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

Closed
wants to merge 4 commits into from
Closed

Conversation

pslacerda
Copy link

@pslacerda pslacerda commented Jun 13, 2018

In #1422 __path__ implicit module attribute is not defined. This fix is not perfect because typing.List[str] would be better.

@pslacerda
Copy link
Author

The last commit was to fix the tests.

Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fix is not perfect because typing.List[str] would be better.

The previous attempt #3527 didn't land exactly because of this. You could read discussions in that PR and improve your solution.

@@ -12,7 +12,7 @@ import m
m.x # E: "object" has no attribute "x"
[file m.py]

[case testListMissingFromStubs]
[case testListMissingFromStubs-skip]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you skip this test?

Copy link
Author

@pslacerda pslacerda Jun 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I thought that was not that important. I think that it can be fixed adding some .pyi stub that define list.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried del list but in mypy del deletes the value and not the variable itself. I would keep as is and mark this test to skip.

[case testModule__path__]
import typing
x = __path__ # type: list
a = __path__ # type: A # E: Incompatible types in assignment (expression has type "list", variable has type "A")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about just using a reveal_type on __path__?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I was only going with the flow of other similar tests. Should them also use reveal_type on __name__ and etc?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As builtins.list is partially typed, reveal_type reveals builtins.list[Any] instead of builtins.list.

@@ -101,7 +101,8 @@ def get_column(self) -> int:
implicit_module_attrs = {'__name__': '__builtins__.str',
'__doc__': None, # depends on Python version, see semanal.py
'__file__': '__builtins__.str',
'__package__': '__builtins__.str'}
'__package__': '__builtins__.str',
'__path__': '__builtins__.list'}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC __path__ is only defined in __init__.py modules, not in every module.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that __path__ was defined everywhere. I will fix that.

@pslacerda
Copy link
Author

#3527 was typing __path__ as __builtins__.str, because of this __path__.append() was failing.
In this PR the variable is correctly typed, even if only partially.

I didn't knew that __path__ was defined only in __init__.pyi? files.

The test was skipped because list was forced to be defined in every module if not previously defined, very much like True.

Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are few more comments. Also I still think it should be List[str], not just list.

# We are running tests without 'list' in builtins.
# TODO: Maybe optionally choose an default fixture stub per test file?
if 'list' not in self.sem.globals:
literal_types.append(('list', AnyType(TypeOfAny.special_form)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very dangerous. With this addition many tests will pass (of fail in ultra-weird way) if someone forgets to add a fixture with list. It is OK with literals, but not with a class.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't list a literal?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it is not, this construct (literal_types) is for things like None, True, False etc.

@@ -20,4 +20,5 @@ class str:
class bytes: pass
class bytearray: pass
class tuple(Generic[T]): pass
class list(Generic[T]): pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The header of this file says # builtins stub with non-generic primitive types, so I would avoid adding generic classes here. We have already enough fixtures with list, why do you need more?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it is a bultin, but I agree that there are enough fixtures.

@@ -101,7 +101,8 @@ def get_column(self) -> int:
implicit_module_attrs = {'__name__': '__builtins__.str',
'__doc__': None, # depends on Python version, see semanal.py
'__file__': '__builtins__.str',
'__package__': '__builtins__.str'}
'__package__': '__builtins__.str',
'__path__': '__builtins__.list'}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you will fix this, you can then just take care to add list.pyi fixture to all tests that include __init__.py[i] files. I think there are not many of those.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you saying that __path__ shoun't be here because it exists only in __init__? So __init__.py[i] that uses __path__ must include an fixture?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please see the previous comments to this code. You already promised to fix it in #5212 (comment) two months ago.

It is OK to just close this if you don't have time/desire to work on this. If you still want to do this, then please go through all previous comments and fix mentioned issues.

@gvanrossum
Copy link
Member

This seems stalled. @pslacerda are you planning to address the issues Ivan mentioned in the code review?

@gvanrossum
Copy link
Member

Let's close this, it's stalled.

@gvanrossum gvanrossum closed this Sep 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants