Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Adds typing_extensions.OrderedDict alias, refs #11528 - #11533

Merged
JelleZijlstra merged 3 commits into
python:masterfrom
sobolevn:issue-11528
Nov 14, 2021
Merged

Adds typing_extensions.OrderedDict alias, refs #11528#11533
JelleZijlstra merged 3 commits into
python:masterfrom
sobolevn:issue-11528

Conversation

@sobolevn

Copy link
Copy Markdown
Member

I went for pythoneval test suite, because all others were very hard to debug / fix.
All of them also required collections.pyi hacks, so it was not worth it.

Closes #11528

@sobolevn

Copy link
Copy Markdown
Member Author
 =================================== FAILURES ===================================
__________________________ SamplesSuite.test_samples ___________________________
[gw0] linux -- Python 3.10.0 /home/runner/work/mypy/mypy/.tox/py/bin/python
Sample check failed
----------------------------- Captured stdout call -----------------------------
mypy/typeshed/stdlib/typing_extensions.pyi:70: error: Cannot assign multiple types to name "OrderedDict" without an explicit "Type[...]" annotation
Found 1 error in 1 file (checked 1 source file)
_______________________ SamplesSuite.test_stdlibsamples ________________________
[gw0] linux -- Python 3.10.0 /home/runner/work/mypy/mypy/.tox/py/bin/python
Sample check failed
----------------------------- Captured stdout call -----------------------------
mypy/typeshed/stdlib/typing_extensions.pyi:70: error: Cannot assign multiple types to name "OrderedDict" without an explicit "Type[...]" annotation
Found 1 error in 1 file (checked 12 source files)
=========================== short test summary info ============================
FAILED mypy/test/testsamples.py::SamplesSuite::test_samples
FAILED mypy/test/testsamples.py::SamplesSuite::test_stdlibsamples
====== 2 failed, 9584 passed, 370 skipped, 7 xfailed in 843.66s (0:14:03) ======
ERROR: InvocationError for command /home/runner/work/mypy/mypy/.tox/py/bin/python -m pytest -n 2 (exited with code 1)

How can I fix it?
I think that I need help here 🙂

@sobolevn

Copy link
Copy Markdown
Member Author

I will ping @JelleZijlstra and @hauntsaninja as both typeshed and mypy experts 🙂

@sobolevn

sobolevn commented Nov 12, 2021

Copy link
Copy Markdown
Member Author

Ok, I've got the simpliest repro:

» mypy mypy/typeshed/stdlib/typing_extensions.pyi
mypy/typeshed/stdlib/typing_extensions.pyi:70: error: Cannot assign multiple types to name "OrderedDict" without an explicit "Type[...]" annotation
Found 1 error in 1 file (checked 1 source file)

@JelleZijlstra

Copy link
Copy Markdown
Member

Sorry, not sure about this. OrderedDict is the only _Alias in typing-extensions, which is probably why we haven't run into this before. Maybe there's some special casing for typing.pyi that wasn't carried over.

@hauntsaninja

Copy link
Copy Markdown
Collaborator

Try messing around with SemanticAnalyzer.prepare_file

@sobolevn

Copy link
Copy Markdown
Member Author

Yes, I should better prepare typing_extensions namespace. I need to remove all AssignmentStmt where we define aliases.

defs.remove(stmt)

Comment thread mypy/semanal.py
# Built-in class target may not ready yet -- defer.
self.create_alias(tree, target_name, alias, name)

def add_typing_extension_aliases(self, tree: MypyFile) -> None:

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.

This mostly duplicates add_builtin_aliases, can you make it take the aliases as a parameter like you did for the prepare method?

@sobolevn sobolevn Nov 13, 2021

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It is a bit different in this part:

           if type_aliases_source_versions[alias] > self.options.python_version:
                # This alias is not available on this Python version.
                continue
            name = alias.split('.')[-1]
            if name in tree.names and not isinstance(tree.names[name].node, PlaceholderNode):
                continue

The thing is: add_builtins_aliases() is used to refine Partial types as well. But, with the new method - we don't have this problem. It also checks for versions while working with typing. But, we do that with typing_extensions. Question: should we?

So, I've decided to devide them, there's no clear way to unify this logic in my head 😞

@JelleZijlstra
JelleZijlstra merged commit f9bf649 into python:master Nov 14, 2021
@sobolevn

Copy link
Copy Markdown
Member Author

Thanks everyone! 🎉

@lmignon

lmignon commented Dec 20, 2021

Copy link
Copy Markdown

Hi,

Is this fix already available into the last release of Mypy? I still have an issue with typing_extensions.OrderedDict

Variable "typing_extensions.OrderedDict" is not valid as a type

Regards

@sobolevn

Copy link
Copy Markdown
Member Author

No, this will be available in 0.930 as far as I know.

tushar-deepsource pushed a commit to DeepSourceCorp/mypy that referenced this pull request Jan 20, 2022
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.

Variable typing_extensions.OrderedDict is not valid as a type

4 participants