Skip to content

Conversation

@Krovatkin
Copy link
Contributor

@Krovatkin Krovatkin commented May 23, 2019

This PR adds a check that prints a warning if a type annotation prefix isn't what mypy expects.

@pytorchbot pytorchbot added the oncall: jit Add this issue/PR to JIT oncall triage queue label May 23, 2019
type_lines = list(filter(lambda line: type_comment in line[1], lines))

if len(type_lines) == 0:
type_pattern = re.compile('#[\t ]*type[\t ]*:')
Copy link
Contributor Author

@Krovatkin Krovatkin May 23, 2019

Choose a reason for hiding this comment

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

I don't know if we necessarily want to support type annotation prefixes that MyPy considers invalid. It might be better to just nudge a user in the right direction, so they fix their prefixes to work with mypy.

Copy link
Contributor

Choose a reason for hiding this comment

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

This regex is a little confusing, won't it also match # type:?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it does, but had there been # type: we wouldn't have even run this regex due to this check : if len(type_lines) == 0:

Copy link
Contributor

@eellison eellison May 23, 2019

Choose a reason for hiding this comment

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

it's valid to have multiple type lines, so it's not enough to check that if there is one valid match. Can we instead filter all lines that include type, check for the exact match, and if there isn't an exact match throw if the regex matches?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this particular case, we would still get Number of type annotations (1) did not match the number of function parameters (2):. We could tweak that error message to advise our user to check their type annotation prefixes.

Copy link
Contributor

Choose a reason for hiding this comment

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

What about

def foo(
   a, # type: int
   b #     type: bad type annotation
)
# type: (...) -> int

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As long as there's at least one valid type annotation prefix, we should get an error.
I just double checked that this exact example also gives the same error.

@torch.jit.script
def add_two(a,# type: int
            b #     type: int
           ):
    # type: (...) -> int
    return a+b

@Krovatkin Krovatkin requested review from driazati and eellison May 23, 2019 21:43
type_pattern = re.compile('#[\t ]*type[\t ]*:')
wrong_type_lines = list(filter(lambda line: type_pattern.search(line[1]), lines))
if len(wrong_type_lines) > 0:
print("WARNING: the annotation prefix in line " + str(wrong_type_lines[0][0])
Copy link
Contributor

Choose a reason for hiding this comment

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

This should either use the actual warnings module or be an error. I'm leaning towards it being an error since people should be fixing this if it comes up.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

let's turn this into an error then.

wrong_type_lines = list(filter(lambda line: type_pattern.search(line[1]), lines))
if len(wrong_type_lines) > 0:
print("WARNING: the annotation prefix in line " + str(wrong_type_lines[0][0])
+ " is invalid. MyPy requires the exact prefix: '# type:'")
Copy link
Contributor

Choose a reason for hiding this comment

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

Also let's not refer to MyPy directly and make people think it's secretly running under the hood

Copy link
Contributor Author

@Krovatkin Krovatkin May 23, 2019

Choose a reason for hiding this comment

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

on the other hand, wouldn't we annoy the heck out of our user if we don't explain her why exactly we don't just support this syntax and raise an error instead? I don't mind removing the mypy comment if you think that's the best course of action here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yea i would rather not reference MyPy directly, there are other python type checkers as well. It's a python standard not a mypy standard

Copy link
Member

Choose a reason for hiding this comment

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

You can refer to the PEP or something lol

@Krovatkin Krovatkin requested a review from driazati May 24, 2019 18:33
Copy link
Member

@suo suo left a comment

Choose a reason for hiding this comment

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

accepting to unblock

@Krovatkin
Copy link
Contributor Author

@pytorchbot rebase this please

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@Krovatkin has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@Krovatkin has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@Krovatkin merged this pull request in cbf2a4f.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merged oncall: jit Add this issue/PR to JIT oncall triage queue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants