On 30/10/2022 17.48, Paulo da Silva wrote:
Às 02:32 de 30/10/22, dn escreveu:
On 30/10/2022 11.59, Paulo da Silva wrote:
Solution (below) will not work if the mention of Foos in GLOBALS is
a forward-reference. Either move GLOBALS to suit, or surround "Foos"
with quotes.
This is the problem for me. So far, without typing, I used to have
some config and globals classes, mostly to just group definitions an
make the program more readable. A matter of taste and style.
Agreed, a good practice.
Thank you.
Now, "typing" is breaking this, mostly because of this forward
reference issue.
As a first step, use the quotation-marks to indicate that such will be
defined later in the code:-
class GLOBALS:
Foos: Optional[Foos]=None
class GLOBALS:
Foos: Optional["Foos"]=None
Later, as gather (typing) expertise, can become more sophisticated,
as-and-when...
The funny thing is that if I replace foos by Foos it works because it
gets known by the initial initialization :-) !
Is the objective to write (good) code, or merely to satisfy the
type-checker?
Something that is misleading is not going to be appreciated by others
(including the +6-months you), eg
a = a + 1 # decrement total
Typing is not compulsory, and has been designed so that we can
implement it a bit at a time, eg only one function amongst many
contained by a module - if that's the only code that requires
maintenance/update.
Best not to create "technical debt" though!
The main idea is to eventually catch some, otherwise "hidden", errors
and produce better and cleaner code. Documentation is also a must.
Good idea!
All-along typing has been regarded as a tool for dev.aids, eg IDEs like
PyCharm and tools such as mypy - rather than a (compulsory) component of
the Python language itself. This means the folk who decide they don't
like the idea can happily (and safely) ignore it - and continue to write
code in perfect-Python. Also, that 'old program[me]s' will continue to
operate, just as well as they ever did, without any reference to the
typing library/ies at all. So, there is no imposed-cost, or 'pressure'
to (wait to) upgrade (such as there was with the jump from Python 2 to
Python 3).
IMHO I'm finding that the auto-checking performed by the IDE is very
helpful and stops my imagination from outstripping my coding-abilities
(in the way that my eyes for chocolate cake are bigger than my
stomach!). I'll go so far as to say that the discipline imposed/flagged
by typing has been more beneficial, than my flirtation with an 'AI
assistant' suggesting what I should code next! The assistant rarely
seems to correctly anticipate my thinking*, whereas typing pulls me up
when the code falls-short and prevents me from falling flat on my face!
* which may say something unfortunate about my coding/design, or may
simply show that SODD (Stack Overflow Driven Development - or the GitHub
equivalent) leaves much to be desired. Hence the gold-plated advice:
never copy-paste code without understanding it first!
As time goes by, 'The Python Gods' have been introducing more and more
capability under the typing banner. Which makes the policy as-outlined,
the sensible (and parallel) course. S/w architecture and coding-quality
books talk about "The Boy Scout Rule" (leave the camp-site in better
condition than you found it). Accordingly, when performing
user-specified updates, while we are 'there', we have the option/ability
to add typing to a module/class/function - just as we might perform
other "refactoring" tasks (one 'green-thumb' I know, referred to it as
'gardening').
Evidently, the OP introduced typing into his/her code-base with the
likes of List*. Thereafter, added the Optional, er, option. Now, we're
talking about forward-references. Plus alluding to more recent
developments (many of which are version-dependent!).
Accordingly, we see another attribute of a gradual-introduction policy -
the practitioner learning and becoming confident that (s)he has mastered
basic techniques, before striding onwards to 'the next level'!
* and for the above reason, I wondered, belatedly, if earlier advice to
'graduate' to "list", might be premature.
PS and on a personal note, this policy is the one I'm following. So,
given that we-two are on the same track, we must be "correct" - and also
the best Python programmers in the world!
(pardon me, I seem over-taken by a coughing fit...)
YMMV!
--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list