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

ParseError: Duplicate top-level identifier #1057

Closed
nirradi opened this issue Nov 19, 2021 · 7 comments
Closed

ParseError: Duplicate top-level identifier #1057

nirradi opened this issue Nov 19, 2021 · 7 comments
Assignees
Labels
bug cat: stubs and 3p type stubs and third-party types

Comments

@nirradi
Copy link

nirradi commented Nov 19, 2021

upgraded from 2021.11.2 to pytype-2021.11.18,
getting this difficult to understand error:

File "common.py", line 3, in <module>: Couldn't import pyi for 'interfaces' [pyi-error]
    File: "interfaces.pyi", line None
  ParseError: Duplicate top-level identifier(s): datetime
@martindemello
Copy link
Contributor

martindemello commented Nov 19, 2021

is the pyi file somewhere we can take a look at it?

the error message is saying that there are two top-level symbols in the module that clash with each other, e.g.

f = ... # type: int
def f(x): ...

we would welcome any suggestions to make it clearer.

@rchen152 rchen152 added bug cat: stubs and 3p type stubs and third-party types labels Nov 19, 2021
@nirradi
Copy link
Author

nirradi commented Nov 21, 2021

its a private repo, here's the top of it with some redaction

# (generated with --quick)

import abc
import datetime
import enum
from typing import Any, Callable, Dict, List, Mapping, Optional, Set, Tuple, Type, TypeVar, Union

XXXXXX = Dict[str, Any]
XXXXXX = Callable[[Dict[str, Any], XXXXXX], XXXXXX]

ABC: Type[abc.ABC]
DataFrame: Any
Flag: Type[enum.Flag]
date: Type[datetime.date]
datetime: Type[datetime.datetime]
time: Type[datetime.time]

here are the original imports in that file

from abc import ABC, abstractmethod
from enum import Flag
from datetime import datetime, time, date
from typing import List, Optional, Union, Callable, Dict
from pandas.core.frame import DataFrame
from dateparser import parse

@csudcy
Copy link

csudcy commented Nov 23, 2021

Yeah, I'm seeing the same issue on 2021.11.18.
Workaround: Downgrade to 2021.11.12 (we don't see the issue in this version anyway).

Definitely the from datetime import datetime that's tripping it up - if I change the import to e.g. from datetime import datetime as datetime2, the error is no longer thrown & the pyi file looks like this:

import datetime
...
datetime2: Type[datetime.datetime]
...

Also, import datetime.datetime (and using datetime.datetime in code) works (though I'm hoping neither of these is the actual solution 😅 ).

@martindemello
Copy link
Contributor

confirmed, thanks for the report! this is caused by the new --gen-stub-imports feature, which puts imports in the module's aliases list, so we now have an alias datetime pointing to the imported module and a constant datetime pointing to datetime.datetime. we probably shouldn't generate an alias for the import since it's a pure from-import.

@rchen152 rchen152 self-assigned this Nov 23, 2021
rchen152 added a commit that referenced this issue Nov 24, 2021
This seems to have caused quite a few breakages. As far as I can tell, it's
because we now handle the 'bidict.bidict' class properly, which is a good
thing, but this has also exposed some sort of weird bug involving overloads and
__init__. I'd like to get a PyPI release out today, so I'm going to stick this
behind a flag for now and dig into it more next week.

For #1057.

PiperOrigin-RevId: 412129190
@rchen152
Copy link
Contributor

I've committed a fix for this, but I had to put it behind a feature flag because it was causing some weird internal test failures. I also just discovered that the feature flag isn't working properly because it isn't getting passed to the type inference step =/ The second issue should be pretty easy to fix, so starting with next week's release, you should be able to pass --fix-module-collisions to pytype to fix this bug. Once I sort out the internal tests, I'll flip the flag to True by default.

@csudcy
Copy link

csudcy commented Nov 25, 2021

Awesome, thanks for looking at it @rchen152 👍

Would it be worth re-opening this issue until the tests are fixed (or at least until the --fix-module-collisions flag is good)?

rchen152 added a commit that referenced this issue Nov 29, 2021
Previously, feature flags were passed only to CHECK, so flags that modify
inference behavior didn't work.

Also bumps __version__ and CHANGELOG so I can release this immediately. (No
point in waiting for nightly testing, since we don't use analyze_project
internally anyway.)

For #1057.

PiperOrigin-RevId: 412996772
@rchen152
Copy link
Contributor

Ah, yeah, I should have reopened the issue when I realized the flag wasn't working - sorry about that! But the flag issue should be fixed with today's release (2021.11.29), so you can now pass --fix-module-collisions to pytype to fix this bug.

fengguang pushed a commit to 0day-ci/linux that referenced this issue Dec 14, 2021
After upgrading mypy and pytype from pip, we see 2 new errors when
running ./tools/testing/kunit/run_checks.py.

Error #1: mypy and pytype
They now deduce that importlib.util.spec_from_file_location() can return
None and note that we're not checking for this.

We validate that the arch is valid (i.e. the file exists) beforehand.
Add in an `asssert spec is not None` to appease the checkers.

Error #2: pytype bug google/pytype#1057
It doesn't like `from datetime import datetime`, specifically that a
type shares a name with a module.

We can workaround this by either
* renaming the import or just using `import datetime`
* passing the new `--fix-module-collisions` flag to pytype.

We pick the first option for now because
* the flag is quite new, only in the 2021.11.29 release.
* I'd prefer if people can just run `pytype <file>`

Signed-off-by: Daniel Latypov <dlatypov@google.com>
ColinIanKing pushed a commit to ColinIanKing/linux-next that referenced this issue Dec 17, 2021
After upgrading mypy and pytype from pip, we see 2 new errors when
running ./tools/testing/kunit/run_checks.py.

Error #1: mypy and pytype
They now deduce that importlib.util.spec_from_file_location() can return
None and note that we're not checking for this.

We validate that the arch is valid (i.e. the file exists) beforehand.
Add in an `asssert spec is not None` to appease the checkers.

Error #2: pytype bug google/pytype#1057
It doesn't like `from datetime import datetime`, specifically that a
type shares a name with a module.

We can workaround this by either
* renaming the import or just using `import datetime`
* passing the new `--fix-module-collisions` flag to pytype.

We pick the first option for now because
* the flag is quite new, only in the 2021.11.29 release.
* I'd prefer if people can just run `pytype <file>`

Signed-off-by: Daniel Latypov <dlatypov@google.com>
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug cat: stubs and 3p type stubs and third-party types
Projects
None yet
Development

No branches or pull requests

4 participants