Skip to content
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

Various improvements to stubgen #7921

Merged
merged 51 commits into from
Nov 14, 2019
Merged

Various improvements to stubgen #7921

merged 51 commits into from
Nov 14, 2019

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Nov 11, 2019

This improves various things in stubgen. Here are some notable changes:

  • Filter out some bad type comments (or comments that look like them) since
    these generate parse errors, and stubgen can't recover from them.
  • Remove the initial generated comment from all stubs, since it gets easily
    out of date and seems redundant, especially for packages with hundreds of
    submodules.
  • Don't generate a blocking error for inconsistent MROs.
  • Make docstring parser more robust.
  • Tweak the level of logging. Add --verbose and --quiet flags.
  • Export imported names that aren't references in the module.
  • Fix handling of unreachable code.
  • Fix signatures of various C extension methods.
  • Skip modules that are not useful or can cause trouble, such as vendored
    modules and tests.
  • Preserve @abstractproperty.
  • Fix clashes between typing.Any and a class named Any (etc.).
  • Translate imports to vendored six to refer to normal six.
  • Add ad-hoc whitelisting of specific definitions that should be exported.

(Even though this is a big PR, most of changes are small and hopefully
this won't be too difficult to review.)

These happen with some frequency in third-party libraries.
We just arbitrarily assume that this means that the module is not
a C module. This happens with paste.
Show the module which we failed to import and hints about what
to do next.
There used to be a bogus "Nothing to do" message from
`mypy.build`.
Add `self` arguments and special casing for many additional dunder
methods.
This may also prevent duplicate definitions from being generated.
However, some definitions that are needed on certain platforms
only or on Python 2 only, for example, may be omitted. This is
arguably less bad than crashing.
Also refactor things a little to make it easier to make the change.
@JukkaL JukkaL requested a review from ilevkivskyi November 11, 2019 14:29
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.

Looks great! Does this need any doc updates, or are they will be in the second PR?

@@ -262,6 +262,8 @@ def __init__(self) -> None:
self.cache_map = {} # type: Dict[str, Tuple[str, str]]
# Don't properly free objects on exit, just kill the current process.
self.fast_exit = False
# Used to transform source code before parsing if not None
self.transform_source = None # type: Optional[Callable[[Any], Any]]
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add a TODO to make this more precise when the mypy issue is fixed?

mypy/parse.py Outdated
if isinstance(source, str): # Work around mypy issue
source = options.transform_source(source)
else:
source = options.transform_source(source)
Copy link
Member

Choose a reason for hiding this comment

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

Is this because of #1533? It looks like this should not be needed with the Any above.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, it's caused by that issue, and this is not actually needed.


def add_file(self, path: str, result: List[str]) -> None:
options = parse_options(flag_list + extra)
if '--verbose' not in flag_list:
Copy link
Member

Choose a reason for hiding this comment

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

Maybe it makes sense to add a test for this flag?

assert not is_test_module('foo.bar')

# The following could be test modules, but we are very conservative and
# don't treat them as such since they could plausibly be real modules.
Copy link
Member

Choose a reason for hiding this comment

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

👍


# Avoid some file names that are unnecessary or likely to cause trouble (\n for end of path).
BLACKLIST = [
'/six.py\n', # Likely vendored six; too dynamic for us to handle
Copy link
Member

Choose a reason for hiding this comment

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

Maybe make these regexps and use $ instead of \n?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll deal with this in a follow-up PR, since the change would cause a merge conflict.

(name == short or
self.import_tracker.reverse_alias.get(name) == short))

def process_member_expr_decorator(self, expr: MemberExpr, context: Decorator) -> bool:
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add a short docstring for this function (and maybe also process_name_expr_decorator)?

@@ -1043,8 +1275,6 @@ def generate_stub_from_ast(mod: StubSource,
if subdir and not os.path.isdir(subdir):
os.makedirs(subdir)
with open(target, 'w') as file:
if add_header:
write_header(file, mod.module, pyversion=pyversion)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we can keep this as opt-in, but make it more informative, like add a creation date, mypy version, etc.?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

TBH, I don't think that the header is useful, since it will likely get stale once the stub is updated.

@JukkaL JukkaL merged commit f86d08e into master Nov 14, 2019
@srittau
Copy link
Contributor

srittau commented Nov 14, 2019

@JukkaL Since @abstractproperty is deprecated, would it make sense to split this into @property @abstractmethod in stubgen?

@JukkaL
Copy link
Collaborator Author

JukkaL commented Nov 14, 2019

Since @abstractproperty is deprecated, would it make sense to split this into @Property @AbstractMethod in stubgen?

A good idea! Can you create an issue to track this? The change itself should be pretty simple.

@JelleZijlstra JelleZijlstra deleted the stubgen-fixes-batch branch November 14, 2019 23:04
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