Skip to content

Defer all types whos metaclass is not ready - #13579

Merged
sobolevn merged 13 commits into
python:masterfrom
sobolevn:metaclass-semanal-failure
Sep 3, 2022
Merged

Defer all types whos metaclass is not ready#13579
sobolevn merged 13 commits into
python:masterfrom
sobolevn:metaclass-semanal-failure

Conversation

@sobolevn

Copy link
Copy Markdown
Member

Finally! I've spent more than 8 hours on this test.
It illustrates a problem with sympy from #13565

@sobolevn
sobolevnforce-pushed the metaclass-semanal-failure branch from 5f310f2 to 8fbd00aCompareSeptember 1, 2022 10:09
@sobolevn

sobolevn commented Sep 1, 2022

Copy link
Copy Markdown
MemberAuthor

I got it right to pass my test, but now we have different problems:

 deferred, incomplete, progress = semantic_analyze_target(
File "/Users/sobolev/Desktop/mypy/mypy/semanal_main.py", line 348, in semantic_analyze_target
infer_decorator_signature_if_simple(node, analyzer)
File "/Users/sobolev/.pyenv/versions/3.8.9/lib/python3.8/contextlib.py", line 131, in __exit__
self.gen.throw(type, value, traceback)
File "/Users/sobolev/Desktop/mypy/mypy/build.py", line 2064, in wrap_context
yield
File "/Users/sobolev/Desktop/mypy/mypy/semanal_main.py", line 339, in semantic_analyze_target
analyzer.refresh_partial(
File "/Users/sobolev/Desktop/mypy/mypy/semanal.py", line 575, in refresh_partial
self.refresh_top_level(node)
File "/Users/sobolev/Desktop/mypy/mypy/semanal.py", line 584, in refresh_top_level
self.add_implicit_module_attrs(file_node)
File "/Users/sobolev/Desktop/mypy/mypy/semanal.py", line 616, in add_implicit_module_attrs
assert isinstance(node, TypeInfo)
AssertionError: 

Big 'thank you' goes to @ilevkivskyi for pushing me in the right direction.
Now I will need to find what is causing this new crash.

@sobolevnsobolevn changed the title Test that illustrates sympy metaclass problemDefer all types whos metaclass is not readySep 1, 2022
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@sobolevn

Copy link
Copy Markdown
MemberAuthor

@ilevkivskyi maybe you have any ideas where we can safely call .add_implicit_module_attrs? Because I am out of ideas. I've tried:

  1. Different places of semanal_main, like: https://github.com/python/mypy/pull/13579/files#diff-2924f9c93ed3b7e350efc02d9068d570cf76f32dc9485fee43df5ee11d4d9ae8R219
  2. SemanticAnalyzer.process_top_level and other methods

Looks like I am stuck: it still fails for some cases 😒

@github-actions

This comment has been minimized.

@ilevkivskyi

Copy link
Copy Markdown
Member

As I mentioned yesterday on discord, you will likely need to special-case builtins SCC, i.e. for this SCC add implicit symbols after processing the whole SCC, but for others keep the things as is.

@github-actions

This comment has been minimized.

1 similar comment
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

1 similar comment
@github-actions

This comment has been minimized.

@sobolevn

Copy link
Copy Markdown
MemberAuthor

Thank you for your guidance! Without it I would spend so much more time.

@ilevkivskyiilevkivskyi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Generally looks good, I have couple more suggestions/ideas.

Comment threadmypy/semanal.py Outdated
# We do it in the very last order, because of
# `builtins.dict <-> typing.Mapping <-> abc.ABCMeta`
# cyclic imports.
if file_node.fullname not in ("typing", "abc", "builtins"):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would instead re-use core_modules (defined in semanal_main.py) here and in other place. Also you may need to use a flag for this method to avoid an import cycle with the latter (like skip_implicit_attrs).

Comment threadmypy/semanal.py Outdated
self.add_implicit_module_attrs(file_node)
# We do it in the very last order, because of
# `builtins.dict <-> typing.Mapping <-> abc.ABCMeta`
# cyclic imports.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The problem is not the cyclic import itself, but that the method assumes that e.g. dict (for __annotations__) would be never marked incomplete (i.e. it may be not defined yet, but as soon as it is, it will be TypeInfo, rather than PlaceholderNode). This makes me think maybe a cleaner solution (if it works), would be just to replace assert with an if (and simply continue if it is a placeholder, like we do if the symbol is None). Can you try this?

Btw curiously this seem to only affect tests for now, it looks in real typeshed currently dict is not an instance of ABCMeta.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Can you try this?

Done, looks like it works! Let's wait to see what tests will show us :)

@sobolevnsobolevn closed this Sep 3, 2022
@sobolevnsobolevn reopened this Sep 3, 2022
@sobolevn

Copy link
Copy Markdown
MemberAuthor

mypy_primer fails with:

 Traceback (most recent call last):
File "/opt/hostedtoolcache/Python/3.10.6/x64/lib/python3.10/site-packages/mypy_primer.py", line 940, in main
retcode = inner()
File "/opt/hostedtoolcache/Python/3.10.6/x64/lib/python3.10/site-packages/mypy_primer.py", line 933, in inner
retcode = asyncio.run(coro)
File "/opt/hostedtoolcache/Python/3.10.6/x64/lib/python3.10/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/opt/hostedtoolcache/Python/3.10.6/x64/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
return future.result()
File "/opt/hostedtoolcache/Python/3.10.6/x64/lib/python3.10/site-packages/mypy_primer.py", line 753, in primer
result = await result_fut
File "/opt/hostedtoolcache/Python/3.10.6/x64/lib/python3.10/asyncio/tasks.py", line 571, in _wait_for_one
return f.result() # May raise f.exception().
File "/opt/hostedtoolcache/Python/3.10.6/x64/lib/python3.10/site-packages/mypy_primer.py", line 414, in primer_result
await self.setup()
File "/opt/hostedtoolcache/Python/3.10.6/x64/lib/python3.10/site-packages/mypy_primer.py", line 345, in setup
repo_dir = await ensure_repo_at_revision(
File "/opt/hostedtoolcache/Python/3.10.6/x64/lib/python3.10/site-packages/mypy_primer.py", line 168, in ensure_repo_at_revision
await clone(repo_url, cwd, shallow=revision_like is None)
File "/opt/hostedtoolcache/Python/3.10.6/x64/lib/python3.10/site-packages/mypy_primer.py", line 134, in clone
await run(cmd, cwd=cwd)
File "/opt/hostedtoolcache/Python/3.10.6/x64/lib/python3.10/site-packages/mypy_primer.py", line 110, in run
raise subprocess.CalledProcessError(proc.returncode, cmd, output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['git', 'clone', '--recurse-submodules', 'https://gitlab.com/dkg/python-sop', '--depth', '1']' returned non-zero exit status 128.

I am sure it is not related, I will try to retrigger it a bit later.

@AlexWaygood

AlexWaygood commented Sep 3, 2022

Copy link
Copy Markdown
Member

mypy_primer fails with:

 Traceback (most recent call last):
File "/opt/hostedtoolcache/Python/3.10.6/x64/lib/python3.10/site-packages/mypy_primer.py", line 940, in main
retcode = inner()
File "/opt/hostedtoolcache/Python/3.10.6/x64/lib/python3.10/site-packages/mypy_primer.py", line 933, in inner
retcode = asyncio.run(coro)
File "/opt/hostedtoolcache/Python/3.10.6/x64/lib/python3.10/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/opt/hostedtoolcache/Python/3.10.6/x64/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
return future.result()
File "/opt/hostedtoolcache/Python/3.10.6/x64/lib/python3.10/site-packages/mypy_primer.py", line 753, in primer
result = await result_fut
File "/opt/hostedtoolcache/Python/3.10.6/x64/lib/python3.10/asyncio/tasks.py", line 571, in _wait_for_one
return f.result() # May raise f.exception().
File "/opt/hostedtoolcache/Python/3.10.6/x64/lib/python3.10/site-packages/mypy_primer.py", line 414, in primer_result
await self.setup()
File "/opt/hostedtoolcache/Python/3.10.6/x64/lib/python3.10/site-packages/mypy_primer.py", line 345, in setup
repo_dir = await ensure_repo_at_revision(
File "/opt/hostedtoolcache/Python/3.10.6/x64/lib/python3.10/site-packages/mypy_primer.py", line 168, in ensure_repo_at_revision
await clone(repo_url, cwd, shallow=revision_like is None)
File "/opt/hostedtoolcache/Python/3.10.6/x64/lib/python3.10/site-packages/mypy_primer.py", line 134, in clone
await run(cmd, cwd=cwd)
File "/opt/hostedtoolcache/Python/3.10.6/x64/lib/python3.10/site-packages/mypy_primer.py", line 110, in run
raise subprocess.CalledProcessError(proc.returncode, cmd, output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['git', 'clone', '--recurse-submodules', 'https://gitlab.com/dkg/python-sop', '--depth', '1']' returned non-zero exit status 128.

I am sure it is not related, I will try to retrigger it a bit later.

We have the same problem on all typeshed PRs at the moment:

@github-actions

This comment has been minimized.

1 similar comment
@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@sobolevn
sobolevn merged commit dfbaff7 into python:masterSep 3, 2022
@sobolevn

Copy link
Copy Markdown
MemberAuthor

Thanks everyone! Next is #13565

Sign up for freeto 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

@sobolevn@ilevkivskyi@AlexWaygood