Ok, so the central part is the funcdef
line:
funcdef: [decorators] 'def' NAME parameters ['->' test] ':' suite
This is saying, "look for an instance of 'def', followed by a NAME
, followed by some parameters
, an optional '->' test
marker (to express the expected return datatype most likely - see here), a literal :
and something called suite
.
The grammatical definition of NAME
is not included in the section you've copied - but it would be some legal string, perhaps not starting with an integer and not overlapping with some additional collection of reserved keywords. I'll leave you to look into that one.
parameters
is defined here, as being some repeating collection of typedargslist
's, surrounded by literal brackets to the left and right.
There's some variation here on how parameters
are expressed that seems to include *
and **
forms of parameter unpacking - that's an interesting aspect of python you can read about here:
The rest is fairly straightforward, but these components described are a minimal definition for how to constitute a function header.
I'd imagine suite
is either a collection of statements that make up a function, or some optional function-header extra that I'm not aware of. You'd have to investigate that. It may be the case that there's some definition function
that's made up of a funcdef
header, followed by some collection of statements with an optional return
element, terminated by some kind of out-dent.
It's difficult to parse only a section of a grammar, so this is the best I can do with what you've provided.
From what I know about how decorators
are applied, it looks like out-denting is already catered for here - so my guess is that suite
in this context describes the function contents as some collection of valid statement phrases.
A comprehensive document that contains hotlinks for each grammar component can be found here which should be able to fill in some of the gaps.
To answer your point about type hinting and return values. Type hinting tells you what you expect your function to return at run-time. If you want to, you can use this information to run compile-time tests and debugging scripts that will identify if/when you're using a function in some unintended way - to be honest, you're unlikely to come across a serious use except in some commercial dev-houses, and they'll have their preferred house-style to inform you how they expect you to use this feature.
Python does not enforce type-hinting at runtime, so the actual return data types for a given function are entirely independent. Type hinting is a late addition to python and to not break anything, was intended to be little more than a useful piece of documentation. Good coding practice would be to use it, but it's little more than a documentary convention.