Skip to content

Misplaced type annotation #5696

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
iddan opened this issue Sep 30, 2018 · 15 comments
Closed

Misplaced type annotation #5696

iddan opened this issue Sep 30, 2018 · 15 comments

Comments

@iddan
Copy link

iddan commented Sep 30, 2018

  • Are you reporting a bug, or opening a feature request? bug
  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.
onboarding_protocols: Dict[str, protocol.RawProtocol] = {
    protocol_path.stem: read_protocol(protocol_path)
    for protocol_path in onboarding_protocols_path.glob('*.json')
}
  • What is the actual behavior/output? mypy emits error:misplaced type annotation
  • What is the behavior/output you expect? to parse correctly
  • What are the versions of mypy and Python you are using? mypy 0.630, Python 3.6.5
    Do you see the same issue after installing mypy from Git master? Yes
  • What are the mypy flags you are using? (For example --strict-optional) None
  • If mypy crashed with a traceback, please paste
    the full traceback below.
@elazarg
Copy link
Contributor

elazarg commented Sep 30, 2018

Seems like a problem in https://github.com/python/typed_ast

@ilevkivskyi
Copy link
Member

I cannot reproduce this. Maybe there is something else you didn't mention?

@iddan
Copy link
Author

iddan commented Oct 1, 2018

Here it is from the beginning of the file (excluding imports):

file_path = Path(__file__)
protocol_schema_path = file_path.parent / "slim_chronic_protocol.schema.json"
onboarding_protocols_path = file_path.parent / 'slim_chronic_onboarding_protocols'
preshare_protocols_path = file_path.parent / 'slim_chronic_preshare_protocols'

with protocol_schema_path.open() as file:
    protocol_schema = json.load(file)

protocol_validator = Draft6Validator(protocol_schema)


def read_protocol(protocol_path: Path) -> protocol.RawProtocol:
    with protocol_path.open() as file:
        raw_protocol = json.load(file)

    protocol_validator.validate(raw_protocol)

    return raw_protocol


onboarding_protocols: Dict[str, protocol.RawProtocol] = {
    protocol_path.stem: read_protocol(protocol_path)
    for protocol_path in onboarding_protocols_path.glob('*.json')
}

preshare_protocols: Dict[str, protocol.RawProtocol] = {
    protocol_path.stem: read_protocol(protocol_path)
    for protocol_path in preshare_protocols_path.glob('*.json')
}

@ilevkivskyi
Copy link
Member

I just tried this, and this works perfectly. What is the version of typed_ast you have?

@iddan
Copy link
Author

iddan commented Oct 1, 2018

1.1.0

@gvanrossum
Copy link
Member

And on what line exactly do you get the error?

@iddan
Copy link
Author

iddan commented Oct 1, 2018

I get the error on the line before preshare_protocols

@ilevkivskyi
Copy link
Member

Are you sure you get the error in this file? Maybe you have a file with a similar name or foo.py vs foo/__init__.py confusion?

@gvanrossum
Copy link
Member

@iddan I think we're not going to get to the bottom of this until you post a completely self-contained repro, something that anybody can copy/paste into a file and run mypy over.

@iddan
Copy link
Author

iddan commented Oct 1, 2018

This is a company secret closed source.

@gvanrossum
Copy link
Member

So you cannot boil it down to something you can share? Then we have to close this issue.

@iddan
Copy link
Author

iddan commented Oct 2, 2018

It was another file. I have no idea why it wasn't reported in VSCode for the wrong location.
Discovered by running mypy from the CLI

@iddan
Copy link
Author

iddan commented Oct 2, 2018

So it seems like commented out code is failing:

# @attrs(auto_attribs=True)
# class Attribute:
#     id: str
#     name: str
#     values: List[Value] = attrib(convert=partial(convert_, Value))
#     type: str = attrib(convert=lambda t: t if isinstance(t, str) else t.name)
#
#     def __contains__(self, item):
#         return item in self.__dict__
#
#     def __getitem__(self, item):
#         return self.__dict__[item]

@ilevkivskyi
Copy link
Member

And this is expected, because on line 6 of your snippet there is

#     type: str

which is a type comment.

You can just put two comment signs to suppress this ## type: str is a normal comment again.

@iddan
Copy link
Author

iddan commented Oct 2, 2018

It will be good if this syntax would be prohibited eventually

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

No branches or pull requests

4 participants