Mypy: Misplaced type annotation

Created on 30 Sep 2018  路  15Comments  路  Source: python/mypy

  • 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.

All 15 comments

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

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

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')
}

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

1.1.0

And on what line exactly do you get the error?

I get the error on the line before preshare_protocols

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?

@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.

This is a company secret closed source.

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

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

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]

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.

It will be good if this syntax would be prohibited eventually

Was this page helpful?
0 / 5 - 0 ratings